3

我正在通过 AsyncTask 内部类向 Yelp API 发出请求。当我尝试在 onPostExecute() 中打印结果时出现错误。这是我的代码:

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;
    }
}
}

还有我的错误信息:

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)
4

3 回答 3

2

那两行:

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

他们正在进行网络操作,这就是您的应用程序崩溃的地方。从构造函数中获取所需的任何引用并在 doInBackground 上进行这些调用

编辑:

digitaljoel 指出日志指向 onPostExecute 行,你唯一合乎逻辑的答案是:

re.getBody()postExecute中的方法执行网络操作。

于 2012-10-26T15:27:32.017 回答
1

onPostExecute()在您的主线程上运行,并且非常不鼓励/不允许在主线程上处理网络操作。将所有网络操作移动到doInBackground()以避免引发此错误

于 2012-10-26T15:13:24.967 回答
0

我猜 Response.getBody 会打开流并读取响应,这就是 android 在主线程上将其标记为网络的原因。您应该从 doInBackground 中的响应中读取字符串并返回 String 而不是 Response 到 onPostExecute,这意味着将声明更改为extends AsyncTask<String,Void,Response>并将 doInBackground 的返回类型更改为String

于 2012-10-26T15:13:31.207 回答