简体   繁体   中英

Json Http request error

Hi I recently combined two of my apps together (which was working fine individually) but when I combined them together I keep getting a JSONParser.makeHttpRequest error.

(This is what logcat showed)

  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)

You are calling "network communication code" on main thread. Use asynctask for that.

Use

new MyAsyncTask().execute();

to start the async task.

Following is a sample of the 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
 }
 }

for fetch url follow this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM