我有一个应用程序,用户在其中提交一些数据,然后将其发送到服务器。我正在平板电脑和 Android 智能手机 (Galaxy S2) 上对其进行测试。在平板电脑上,只要我单击“提交”,应用程序就会停止工作,并显示“不幸的是已停止工作”消息。这个问题在手机或模拟器上都看不到,这让我很困惑。
应用程序中还有另一个屏幕,用户可以选择重新提交相同的凭据。在那里也遇到了同样的问题。该应用程序的其余部分工作正常。这使我得出结论,问题可能在于我将数据发送到服务器的方式。该代码片段如下:
//code to send to server should begin here.
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost("http://www.mywebsite.com/takeDetails.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
String val = "new";
nameValuePairs.add(new BasicNameValuePair("mode", val));
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("number", number));
nameValuePairs.add(new BasicNameValuePair("email", emailID));
Log.v(this.toString(), "Email = " + emailID);
hp.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = hc.execute(hp);
//Toast.makeText(getApplicationContext(), "Attempting to register.", Toast.LENGTH_LONG).show();
String responseBody = EntityUtils.toString(response.getEntity());
if(responseBody.contains("Success")) {
Toast.makeText(getApplicationContext(), "Thank you for registering! You will receive an email with your username and password shortly.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Attempt to register failed.", Toast.LENGTH_LONG).show();
}
Log.v(this.toString(), "HTTP Response = " + responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
日志输出:
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Name: jguyjfhf
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Number: 668895898
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Email ID:jvjhfhc@ccf.mkj
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): User details gleaned = Name = jguyjfhf
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): 668895898
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): jvjhfhc@ccf.mkj
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Email = jvjhfhc@ccf.mkj
D/AndroidRuntime( 3766): Shutting down VM
W/dalvikvm( 3766): threadid=1: thread exiting with uncaught exception (group=0x409f11f8)
E/AndroidRuntime( 3766): FATAL EXCEPTION: main
E/AndroidRuntime( 3766): android.os.NetworkOnMainThreadException
E/AndroidRuntime( 3766): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
E/AndroidRuntime( 3766): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
E/AndroidRuntime( 3766): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
E/AndroidRuntime( 3766): at java.net.InetAddress.getAllByName(InetAddress.java:220)
E/AndroidRuntime( 3766): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
E/AndroidRuntime( 3766): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/AndroidRuntime( 3766): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/AndroidRuntime( 3766): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/AndroidRuntime( 3766): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/AndroidRuntime( 3766): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/AndroidRuntime( 3766): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E/AndroidRuntime( 3766): at com.sriram.htmldisplay.htmlDisplay.writeSendDetails(htmlDisplay.java:200)
E/AndroidRuntime( 3766): at com.sriram.htmldisplay.htmlDisplay.access$10(htmlDisplay.java:127)
E/AndroidRuntime( 3766): at com.sriram.htmldisplay.htmlDisplay$1.onClick(htmlDisplay.java:110)
E/AndroidRuntime( 3766): at android.view.View.performClick(View.java:3511)
E/AndroidRuntime( 3766): at android.view.View$PerformClick.run(View.java:14105)
E/AndroidRuntime( 3766): at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime( 3766): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3766): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3766): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 3766): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3766): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 3766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 3766): at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm( 3766): GC_CONCURRENT freed 290K, 7% free 6697K/7175K, paused 4ms+6ms
W/ActivityManager( 1268): Force finishing activity com.sriram.htmldisplay/.htmlDisplay
D/TabletStatusBar( 1340): hiding the MENU button
W/ActivityManager( 1268): Activity pause timeout for ActivityRecord{41406c60 com.sriram.htmldisplay/.htmlDisplay
我的问题:
1. 有没有更好的方法来处理来自 HTTPClient 的错误?
2. 任何关于可能导致平板电脑出现故障的想法都非常受欢迎。