2013-04-26 86 views
0

编辑: 我终于弄清楚问题所在。我不断收到此线上的IOException response = httpClient.execute(httpPost)。发送用户注册数据到服务器

但是,会导致此错误的正常问题(我在网上搜索的问题,例如url被错误等)在这里不是问题。该网址是正确的。任何人都可以帮助或列出所有可能的原因,我会得到这个错误。谢谢。


我知道这个问题已经被问了好几次,我竟然看的,为了这些问题给出的答案,原来弄清楚如何做到这一点,但由于某种原因,它不工作,我不知道为什么。

我有一个需要用户注册的应用程序。他们注册后,我发送他们输入到服务器的信息。

的Android代码:

//This is the method called when user presses submit button. The variables not declared 
//are global 
public void registerUser(View v){ 
    context = getApplicationContext(); 

    username = ((EditText) this.findViewById(R.id.username)).getText().toString(); 
    email = ((EditText) this.findViewById(R.id.email)).getText().toString(); 
    password=((EditText)this.findViewById(R.id.password)).getText().toString(); 

    String[] params = {username,email,password}; 
    (new SendRegisterInfo(this.getApplicationContext())).execute(params); 

    Toast t = Toast.makeText(context, "Thank you for Signing up "+username, Toast.LENGTH_SHORT); 
    t.show(); 
    //Start Main Page Activity. Page they'll see everytime they login 
    Intent i = new Intent(this,HomeActivity.class); 
    startActivity(i); 

} 


public class SendRegisterInfo extends AsyncTask<String,Void,Void>{ 

private String tag = "SendRegisterInfo"; 
private InputStream is; 
private Context c; 

@Override 
protected void onPostExecute(Void result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
} 

public SendRegisterInfo(Context c){ 
    this.c = c; 
} 

@Override 
protected Void doInBackground(String... params) { 
    String URI="http://www.mysite.com/registerUser.php"; 

    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(URI); 
    HttpResponse response; 

    String username,email,password; 
    username = params[0]; 
    email = params[1]; 
    password = params[2]; 


    try { 

     ArrayList<NameValuePair> submit = new ArrayList<NameValuePair>(2); 
     submit.add(new BasicNameValuePair("username",username)); 
     submit.add(new BasicNameValuePair("email",email)); 
     submit.add(new BasicNameValuePair("password",password)); 
     httpPost.setEntity(new UrlEncodedFormEntity(submit)); 

     response = httpClient.execute(httpPost); 
     Log.i(tag,response.getStatusLine().toString()); 

    } catch (ClientProtocolException e) { 
     Log.e(tag, "Error in http connection "+e.toString()); 

     e.printStackTrace(); 
    } catch (IOException e) { 
     Log.e(tag, "Error in http connection "+e.toString()); 

     e.printStackTrace(); 
    }catch (Exception e){ 
     Log.e(tag, "Error in http connection "+e.toString()); 

     e.printStackTrace(); 
    } 

    return null; 
} 


} 

清单文件:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

PHP代码:

<?php 
$username = $_POST["username"]; 
$email = $_POST["email"]; 
$pswrd = $_POST["password"]; 
$score = 0; 

$user = "root"; 
$password="pword"; 
$database = "databasename"; 

mysql_connect(localhost,$user,$password); 

mysql_select_db($database) or die("Unable to Select Database"); 

$query="INSERT INTO Players VALUES ('','$username','$email','$pswrd','','$score')"; 

mysql_query($query); 

mysql_close(); 

?> 
+0

你说这是工作,你不知道为什么? – Jordan 2013-04-26 21:22:50

+0

什么不工作?尝试调试你的代码..如果你创建一个简单的表单并发布数据,是否需要php脚本工作? Android是否实际发送内容?你有任何错误? – YuviDroid 2013-04-26 22:03:30

+1

我通过快速查看代码看到的唯一问题是您在UI线程上执行网络活动(除非您没有发布完整的代码)。 – YuviDroid 2013-04-26 22:05:39

回答

0

你有一个错误读取(字符串... PARAMS)的说法doInBackground()方法。

当您启动的AsyncTask:当你读的论点

String username,email,weight,password; 
username = params[0]; 
email = params[1]; 
password = params[3]; // should be: password = params[2]; 

如果这不是作品

String[] params = {username,email,password}; 

,增加了MySQL查询要求对PHP程序的最后一个插入ID并打印结果以获得客户端的响应:

response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity();  
Log.i(tag,EntityUtils.toString(entity).toString()); 

所以你可以看看你是否插入或不在数据库上。

+0

感谢您的回复。你第一次列出的错误实际上是我的错别字。我也找到了错误的地方,然后再次编辑问题。 – evthim 2013-04-29 23:10:13

0

这是你的问题:

RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

你试图表现出与不是用来显示吐司类敬酒。或者,也许还没有准备好展示一个东西。什么是包含该方法的类registerUser

+0

谢谢你的回应,那实际上是其中的一个错误。我修复了它,并且应用程序停止了崩溃,但数据仍未被发送到服务器。显然主要的问题是这条线路响应= httpClient.execute(httpPost),给我一个IOException。我已经更新了这个问题。 – evthim 2013-04-29 23:08:58

0

请检查下面的代码。

  • 为请求添加函数名称和密钥以及您的数据。

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
    
    nameValuePairs.add((NameValuePair) new BasicNameValuePair("f", "yourFunctionName."+methodName)); 
    
    nameValuePairs.add((NameValuePair) new BasicNameValuePair("u", "a0ff8a6a0a831ec25cf4de6c730be54c")); 
    
    //u is the key used by server to identify valid request.