A H
A H

Reputation: 245

I'm using AsyncTask and getting threading errors

I'm making a request to the Yelp API through an AsyncTask innerclass. When I try to print the result in onPostExecute() i get errors. Heres my code:

public class MainActivity extends Activity {

Yelp yelp = new yelp(....);
yelp.execute();
public class Yelp extends AsyncTask<String,Void,Response> {
    double _long = 1;
    double _lat= 1;
    String _term = "sushi";     
  OAuthService service;
  Token accessToken;
  public Yelp(String consumerKey, String consumerSecret, String token, String tokenSecret) {
    this.service = new ServiceBuilder().provider(YelpApi2.class).apiKey(consumerKey).apiSecret(consumerSecret).build();
    this.accessToken = new Token(token, tokenSecret);
  }

    @Override
    protected void onPostExecute(Response re) {
        try{
            System.out.println(re.getBody());
        }
        catch(Exception e){
            e.printStackTrace();
        }
     }

    @Override
    protected Response doInBackground(String... params) {
        OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
        request.addQuerystringParameter("term", _term);
        request.addQuerystringParameter("ll", _lat + "," + _long);
        this.service.signRequest(this.accessToken, request);
        Response r = request.send();
        return r;
    }
}
}

And my error msgs:

10-26 10:55:45.630: W/System.err(1060): android.os.NetworkOnMainThreadException
10-26 10:55:45.640: W/System.err(1060):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-26 10:55:45.640: W/System.err(1060):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
10-26 10:55:45.640: W/System.err(1060):     at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
10-26 10:55:45.640: W/System.err(1060):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
10-26 10:55:45.640: W/System.err(1060):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
10-26 10:55:45.651: W/System.err(1060):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
10-26 10:55:45.651: W/System.err(1060):     at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
10-26 10:55:45.651: W/System.err(1060):     at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
10-26 10:55:45.651: W/System.err(1060):     at java.io.InputStreamReader.read(InputStreamReader.java:244)
10-26 10:55:45.651: W/System.err(1060):     at org.scribe.utils.StreamUtils.getStreamContents(StreamUtils.java:29)
10-26 10:55:45.651: W/System.err(1060):     at org.scribe.model.Response.parseBodyContents(Response.java:40)
10-26 10:55:45.651: W/System.err(1060):     at org.scribe.model.Response.getBody(Response.java:66)
10-26 10:55:45.660: W/System.err(1060):     at com.example.first.MainActivity$Yelp.onPostExecute(MainActivity.java:109)
10-26 10:55:45.670: W/System.err(1060):     at com.example.first.MainActivity$Yelp.onPostExecute(MainActivity.java:1)
10-26 10:55:45.670: W/System.err(1060):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-26 10:55:45.670: W/System.err(1060):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-26 10:55:45.670: W/System.err(1060):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-26 10:55:45.681: W/System.err(1060):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 10:55:45.690: W/System.err(1060):     at android.os.Looper.loop(Looper.java:137)
10-26 10:55:45.690: W/System.err(1060):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-26 10:55:45.710: W/System.err(1060):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 10:55:45.710: W/System.err(1060):     at java.lang.reflect.Method.invoke(Method.java:511)
10-26 10:55:45.720: W/System.err(1060):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-26 10:55:45.720: W/System.err(1060):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 10:55:45.730: W/System.err(1060):     at dalvik.system.NativeStart.main(Native Method)
10-26 10:59:03.251: D/testtt(1109): BUS3333333TEDDDD
10-26 10:59:03.351: D/ddd(1109): BUSTEDDDD
10-26 10:59:03.521: D/gralloc_goldfish(1109): Emulator without GPU emulation detected.
10-26 10:59:05.431: D/dalvikvm(1109): GC_FOR_ALLOC freed 174K, 4% free 8126K/8391K, paused 53ms, total 56ms
10-26 10:59:05.440: W/System.err(1109): android.os.NetworkOnMainThreadException
10-26 10:59:05.451: W/System.err(1109):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-26 10:59:05.451: W/System.err(1109):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
10-26 10:59:05.451: W/System.err(1109):     at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
10-26 10:59:05.461: W/System.err(1109):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
10-26 10:59:05.461: W/System.err(1109):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
10-26 10:59:05.471: W/System.err(1109):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
10-26 10:59:05.471: W/System.err(1109):     at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
10-26 10:59:05.471: W/System.err(1109):     at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
10-26 10:59:05.481: W/System.err(1109):     at java.io.InputStreamReader.read(InputStreamReader.java:244)
10-26 10:59:05.481: W/System.err(1109):     at org.scribe.utils.StreamUtils.getStreamContents(StreamUtils.java:29)
10-26 10:59:05.481: W/System.err(1109):     at org.scribe.model.Response.parseBodyContents(Response.java:40)
10-26 10:59:05.492: W/System.err(1109):     at org.scribe.model.Response.getBody(Response.java:66)
10-26 10:59:05.492: W/System.err(1109):     at com.example.first.MainActivity$Yelp.onPostExecute(MainActivity.java:104)
10-26 10:59:05.492: W/System.err(1109):     at com.example.first.MainActivity$Yelp.onPostExecute(MainActivity.java:1)
10-26 10:59:05.501: W/System.err(1109):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-26 10:59:05.501: W/System.err(1109):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-26 10:59:05.501: W/System.err(1109):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-26 10:59:05.501: W/System.err(1109):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 10:59:05.511: W/System.err(1109):     at android.os.Looper.loop(Looper.java:137)
10-26 10:59:05.511: W/System.err(1109):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-26 10:59:05.521: W/System.err(1109):     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 10:59:05.521: W/System.err(1109):     at java.lang.reflect.Method.invoke(Method.java:511)
10-26 10:59:05.521: W/System.err(1109):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-26 10:59:05.521: W/System.err(1109):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 10:59:05.531: W/System.err(1109):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 3

Views: 1176

Answers (3)

Budius
Budius

Reputation: 39836

those 2 lines:

this.service = new ServiceBuilder().provider(YelpApi2.class).apiKey(consumerKey).apiSecret(consumerSecret).build();
this.accessToken = new Token(token, tokenSecret);

they are doing networking operations, that's where your app is crashing. Get whatever reference you need from the constructor and make those calls on the doInBackground

edit:

digitaljoel pointed that the log points to the onPostExecute line, thou the only logical answer is:

the method re.getBody() inside your postExecute performs network operation.

Upvotes: 2

digitaljoel
digitaljoel

Reputation: 26574

I'm guessing that Response.getBody opens the stream and reads the response, which is why android is flagging it as network on the main thread. You should read the string from your response in your doInBackground and return the String instead of the Response to onPostExecute, which means changing the declaration to extends AsyncTask<String,Void,Response> and the return type of doInBackground to String.

Upvotes: 0

burmat
burmat

Reputation: 2548

onPostExecute() runs on your main thread, and proccessing network operations on the main thread is highly discouraged/not allowed. Move all networking operations to doInBackground() to avoid this error being thrown

Upvotes: 1

Related Questions