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)