我有以下 java 代码:
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.105/test.php");
List<NameValuePair> teste = new ArrayList<NameValuePair>(1);
teste.add(new BasicNameValuePair("test", "test"));
httppost.setEntity(new UrlEncodedFormEntity(test));
HttpResponse response = (HttpResponse) httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
每当我取消注释/评论行HttpResponse response = (HttpResponse) httpclient.execute(httppost);
时,应用程序崩溃/不会崩溃。 我在清单上有互联网权限,我想从服务器获取json_encoded消息。我试过&#34;一切&#34;但仍无法将其投入使用。
日志:
07-10 15:04:30.197:E / AndroidRuntime(19382):致命异常:主要 07-10 15:04:30.197:E / AndroidRuntime(19382): java.lang.IllegalStateException:无法执行的方法 活动07-10 15:04:30.197:E / AndroidRuntime(19382):at android.view.View $ 1.onClick(View.java:3591)07-10 15:04:30.197: E / AndroidRuntime(19382):at android.view.View.performClick(View.java:4084)07-10 15:04:30.197: E / AndroidRuntime(19382):at android.view.View $ PerformClick.run(View.java:16966)07-10 15:04:30.197:E / AndroidRuntime(19382):at android.os.Handler.handleCallback(Handler.java:615)07-10 15:04:30.197:E / AndroidRuntime(19382):at android.os.Handler.dispatchMessage(Handler.java:92)07-10 15:04:30.197:E / AndroidRuntime(19382):at android.os.Looper.loop(Looper.java:137)07-10 15:04:30.197: E / AndroidRuntime(19382):at android.app.ActivityThread.main(ActivityThread.java:4745)07-10 15:04:30.197:E / AndroidRuntime(19382):at java.lang.reflect.Method.invokeNative(Native Method)07-10 15:04:30.197:E / AndroidRuntime(19382):at java.lang.reflect.Method.invoke(Method.java:511)07-10 15:04:30.197: E / AndroidRuntime(19382):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 07-10 15:04:30.197:E / AndroidRuntime(19382):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)07-10 15:04:30.197:E / AndroidRuntime(19382):at dalvik.system.NativeStart.main(Native Method)07-10 15:04:30.197: E / AndroidRuntime(19382):引起: java.lang.reflect.InvocationTargetException 07-10 15:04:30.197: E / AndroidRuntime(19382):at java.lang.reflect.Method.invokeNative(Native Method)07-10 15:04:30.197:E / AndroidRuntime(19382):at java.lang.reflect.Method.invoke(Method.java:511)07-10 15:04:30.197: E / AndroidRuntime(19382):at android.view.View $ 1.onClick(View.java:3586)07-10 15:04:30.197: E / AndroidRuntime(19382):... 11更多07-10 15:04:30.197: E / AndroidRuntime(19382):引起: android.os.NetworkOnMainThreadException 07-10 15:04:30.197: E / AndroidRuntime(19382):at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 07-10 15:04:30.197:E / AndroidRuntime(19382):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)07-10 15:04:30.197:E / AndroidRuntime(19382):at libcore.io.IoBridge.connectErrno(IoBridge.java:127)07-10 15:04:30.197:E / AndroidRuntime(19382):at libcore.io.IoBridge.connect(IoBridge.java:112)07-10 15:04:30.197: E / AndroidRuntime(19382):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)07-10 15:04:30.197:E / AndroidRuntime(19382):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)07-10 15:04:30.197:E / AndroidRuntime(19382):at java.net.Socket.connect(Socket.java:842)07-10 15:04:30.197: E / AndroidRuntime(19382):at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 07-10 15:04:30.197:E / AndroidRuntime(19382):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 07-10 15:04:30.197:E / AndroidRuntime(19382):at com.diverse.just.LoginActivity.newAccount(LoginActivity.java:92)07-10 15:04:30.197:E / AndroidRuntime(19382):... 14更多
答案 0 :(得分:1)
您必须在另一个线程上执行HTTP请求部分。
例如:
AsyncTask<String, Void, Void> db = null;
try {
db = new db_conn(this);
db.execute("http://192.168.1.5/request.php").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
.get()方法在主线程继续执行之前等待AsyncTask完成。
db class:
class db_conn extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
GetDataFromDB db = new GetDataFromDB();
String url = params[0];
System.out.println(url);
jObject = db.makeHttpRequest(url, "GET", null);
return null;
}
....
.....
}
答案 1 :(得分:0)
Caused by: android.os.NetworkOnMainThreadException
,您无法在主线程上执行网络请求。将其移至背景。
答案 2 :(得分:0)