Json Http请求错误

时间:2015-09-23 05:15:27

标签: android json

您好我最近将我的两个应用程序组合在一起(单独工作正常)但是当我将它们组合在一起时,我不断收到JSONParser.makeHttpRequest错误。

(这是logcat显示的内容)

  android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperato...:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at info.androidhive.loginandregistration.JSONParser.makeHttpRequest(JSONParser.java:62)
at info.androidhive.loginandregistration.EditProductActivity$GetProductDetails$1.run(EditProductActivity.java:106)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您正在呼叫"网络通信代码"在主线程上。使用asynctask。

使用

new MyAsyncTask().execute();

启动异步任务。

以下是asynctask

的示例
private class MyAsyncTask extends AsyncTask<String, String, String> {
private HashMap<String, String> keyValue = new HashMap<String, String>();
protected void onPreExecute() {

    //initialise variable

    super.onPreExecute();


    keyValue.put("user_name", name);
    keyValue.put("password", password);
    }
 protected Long doInBackground(String... params) {
    // do your long running process here
         try {
        FetchUrl findLocation = new FetchUrl();
        response = findLocation.fetchUrl(url, keyValue);
    } catch (Exception e) {
        e.printStackTrace();
        StackTraceElement[] ele = e.getStackTrace();

    }
    return response;
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
      // publish the progress of the task
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     //code here for result on main thread
 }
 }

获取网址跟随this