2013-04-26 76 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.