应用程序在姜饼中正常运行但在kikat中死亡。这是logcat:
06-06 07:57:21.337: E/AndroidRuntime(1038): Process: com.example.barcodescanningapp, PID: 1038
06-06 07:57:21.337: E/AndroidRuntime(1038): android.os.NetworkOnMainThreadException
06-06 07:57:21.337: E/AndroidRuntime(1038): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
06-06 07:57:21.337: E/AndroidRuntime(1038): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-06 07:57:21.337: E/AndroidRuntime(1038): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-06 07:57:21.337: E/AndroidRuntime(1038): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-06 07:57:21.337: E/AndroidRuntime(1038): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-06 07:57:21.337: E/AndroidRuntime(1038): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-06 07:57:21.337: E/AndroidRuntime(1038): at java.net.Socket.connect(Socket.java:843)
06-06 07:57:21.337: E/AndroidRuntime(1038): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-06 07:57:21.337: E/AndroidRuntime(1038): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-06 07:57:21.337: E/AndroidRuntime(1038): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
错误似乎与网络请求有关,但为什么会发生在kikat而不是gingenbread。
网络请求代码:
item_name_view = (EditText) findViewById(R.id.item_name);
item_quantity_view = (EditText) findViewById(R.id.item_quantity);
item_name = item_name_view.getText().toString();
Log.w("item name", item_name);
item_quantity = item_quantity_view.getText().toString();
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(SUBMISSION_URL);
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
nameValuePair.add(new BasicNameValuePair("code", scanContent));
nameValuePair.add(new BasicNameValuePair("name", item_name));
nameValuePair.add(new BasicNameValuePair("quantity", item_quantity));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
//response_text.setText(getResponseBody(response).replaceAll("<.*>", " "));
response_text.setText(getResponseBody(response));
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
应用程序通过邮件向服务器发出请求,并接收要在活动中显示的响应。这对姜饼坚果应用程序的工作正常,当请求进入网络时会崩溃。