2013-03-07 275 views
1

我使用最新的Java Eclipse軟件,當我運行此HttpPost代碼時,模擬器崩潰。我在我的筆記本電腦上安裝了uniserver,因此我將它用作服務器。Android HttpPost。將數據發送到PHP服務器上的表單

此代碼應該從以前的類調用編輯文本數據,並且使用HttpPost請求這個數據上傳到在線表單上各自的領域。

編輯的文本數據是3個字段:「從」,「To」和「消息」。而且我在服務器上創建的表單也有這些相同的字段來輸入數據。 (「http://19x.xx.xx.xxx/androidp2p/testform.php」)其中,19x.xx.xx.xxx是我的(本地主機)IP地址。

我正確地從以前的類拉動的數據和我的代碼類似於HttpPost的例子,我在網上找到,但我不知道爲什麼會崩潰。

我附HttpPost方法,我想看看我是否能得到任何援助。提前感謝你。

方法1:

String myBreadu, myBreadr, myBreadm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Bundle myBasket = getIntent().getExtras(); 
    myBreadu = myBasket.getString("keyfrom"); 
    myBreadr = myBasket.getString("keyto"); 
    myBreadm = myBasket.getString("keymsg"); 
    // Create a new HttpClient and Post Header 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = ("http://19x.xx.xx.xxx/androidp2p/testform.php"); 
    HttpPost post = new HttpPost(postURL); 
    try { 
     // Add the data 
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(3); 
     pairs.add(new BasicNameValuePair("keysendu", myBreadu)); 
     pairs.add(new BasicNameValuePair("keysendr", myBreadr)); 
     pairs.add(new BasicNameValuePair("keysendm", myBreadm)); 
     UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs); 
     post.setEntity(uefe); 
     // Execute the HTTP Post Request 
     HttpResponse response = client.execute(post); 
     // Convert the response into a String 
     HttpEntity resEntity = response.getEntity(); 
     if (resEntity != null) { 
      Log.i("RESPONSE", EntityUtils.toString(resEntity)); 
     } 
    } catch (UnsupportedEncodingException uee) { 
     uee.printStackTrace(); 
    } catch (ClientProtocolException cpe) { 
     cpe.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 

} 

方法2:

String myBreadu, myBreadr, myBreadm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Bundle myBasket = getIntent().getExtras(); 
    myBreadu = myBasket.getString("keyfrom"); 
    myBreadr = myBasket.getString("keyto"); 
    myBreadm = myBasket.getString("keymsg"); 
    String result = null; 
    // Create a new HttpClient and Post Header 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = ("http://186.45.107.129/androidp2p/testform.php"); 
    HttpPost post = new HttpPost(postURL); 
    try { 
     // Add the data 
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(3); 
     pairs.add(new BasicNameValuePair("keysendu", myBreadu)); 
     pairs.add(new BasicNameValuePair("keysendr", myBreadr)); 
     pairs.add(new BasicNameValuePair("keysendm", myBreadm)); 
     UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs); 
     post.setEntity(uefe); 
     // Execute the HTTP Post Request 
     HttpResponse response = client.execute(post); 
     // Convert the response into a String 
     HttpEntity resEntity = response.getEntity(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     String l = ""; 
     StringBuilder sb = new StringBuilder(); 
     while ((l = rd.readLine()) != null) { 
      sb.append(l + "\n"); 
     } 
     rd.close(); 
     String result = sb.toString(); // this line gives an error "Duplicate local variable result" 
    } catch (UnsupportedEncodingException uee) { 
     uee.printStackTrace(); 
    } catch (ClientProtocolException cpe) { 
     cpe.printStackTrace(); 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } 

} 

這是testform.PHP

測試表單

來源:
爲:
消息:

我可以補充一點好嗎?我不知道我是否應該被直接發送數據到窗體或這個其他PHP頁面我有..

通過我在日誌中得到錯誤,當我嘗試HttpPost的方式是:

FATAL EXCEPTION: main 

03-07 11:36:23.226: E/AndroidRuntime(1490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.keegan/com.project.keegan.SendPostMethod}: android.os.NetworkOnMainThreadException 

03-07 11:36:23.226: E/AndroidRuntime(1490):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 

03-07 11:36:23.226: E/AndroidRuntime(1490):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 

03-07 11:36:23.226: E/AndroidRuntime(1490):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 

對不起,如果這是太多的信息傢伙。謝謝。

+0

不,信息是永遠不嫌多的任何地方調用它! :) – Swayam 2013-03-07 16:06:02

回答

1

您需要使用AsyncTask才能完成所有網絡操作。

您的網絡操作會佔用大量的時間,如果它是在主UI線程上完成的UI會得到響應。如果您的用戶界面長時間凍結,該應用可能會被操作系統殺死。

因此的Android 4+使得強制使用後臺線程執行網絡操作。

把代碼做的網絡活動中doInBacground()和使用​​所有的AsyncTask。

這是你的AsyncTask會是什麼樣子:

private class SendData extends AsyncTask<String, Integer, Void> { 
     protected void doInBackground() { 
// Create a new HttpClient and Post Header 
HttpClient client = new DefaultHttpClient(); 
String postURL = ("http://19x.xx.xx.xxx/androidp2p/testform.php"); 
HttpPost post = new HttpPost(postURL); 
try { 
    // Add the data 
    List<NameValuePair> pairs = new ArrayList<NameValuePair>(3); 
    pairs.add(new BasicNameValuePair("keysendu", myBreadu)); 
    pairs.add(new BasicNameValuePair("keysendr", myBreadr)); 
    pairs.add(new BasicNameValuePair("keysendm", myBreadm)); 
    UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(pairs); 
    post.setEntity(uefe); 
    // Execute the HTTP Post Request 
    HttpResponse response = client.execute(post); 
    // Convert the response into a String 
    HttpEntity resEntity = response.getEntity(); 
    if (resEntity != null) { 
     Log.i("RESPONSE", EntityUtils.toString(resEntity)); 
    } 
} catch (UnsupportedEncodingException uee) { 
    uee.printStackTrace(); 
} catch (ClientProtocolException cpe) { 
    cpe.printStackTrace(); 
} catch (IOException ioe) { 
    ioe.printStackTrace(); 
} 
        
 } 

 protected void onProgressUpdate() { 
    //called when the background task makes any progress 
 } 

  protected void onPreExecute() { 
     //called before doInBackground() is started 
 } 
 protected void onPostExecute() { 
     //called after doInBackground() has finished  
 } 
  } 

,並且可以使用new SendData().execute("");

+0

非常感謝你swayam,應用程序不會再崩潰。但是,沒有數據顯示在PHP表單上。 – Keegs 2013-03-08 00:03:52

+0

這是錯誤日誌消息:E /跟蹤(1175):錯誤打開跟蹤文件:沒有這樣的文件或目錄(2) – Keegs 2013-03-08 00:23:16

+0

它通常是更好地使每交一個問題,因爲它更容易讓別人理解你的問題。這篇文章是關於應用程序崩潰,它已被修復。所以,我勸你再拍後與新的問題和詳細的logcat輸出。並且不要忘記*接受/贊成*我的回答。 – Swayam 2013-03-08 08:09:04

相關問題