问题
I am attempting to integrate with the Yelp API for Android.
I keep getting fatal errors whenever I am trying to connect.
10-14 18:32:29.207: E/AndroidRuntime(7354): FATAL EXCEPTION: main
10-14 18:32:29.207: E/AndroidRuntime(7354): java.lang.RuntimeException: Unable to start activity ComponentInfo{cse.usf.edu.android/cse.usf.edu.android.HelloWorldActivity}: android.os.NetworkOnMainThreadException
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.ActivityThread.access$600(ActivityThread.java:139)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.os.Looper.loop(Looper.java:154)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.ActivityThread.main(ActivityThread.java:4945)
10-14 18:32:29.207: E/AndroidRuntime(7354): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 18:32:29.207: E/AndroidRuntime(7354): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 18:32:29.207: E/AndroidRuntime(7354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-14 18:32:29.207: E/AndroidRuntime(7354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-14 18:32:29.207: E/AndroidRuntime(7354): at dalvik.system.NativeStart.main(Native Method)
10-14 18:32:29.207: E/AndroidRuntime(7354): Caused by: android.os.NetworkOnMainThreadException
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
10-14 18:32:29.207: E/AndroidRuntime(7354): at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
10-14 18:32:29.207: E/AndroidRuntime(7354): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
10-14 18:32:29.207: E/AndroidRuntime(7354): at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-14 18:32:29.207: E/AndroidRuntime(7354): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-14 18:32:29.207: E/AndroidRuntime(7354): at org.scribe.model.Response.<init>(Response.java:27)
10-14 18:32:29.207: E/AndroidRuntime(7354): at org.scribe.model.Request.doSend(Request.java:110)
10-14 18:32:29.207: E/AndroidRuntime(7354): at org.scribe.model.Request.send(Request.java:62)
10-14 18:32:29.207: E/AndroidRuntime(7354): at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
10-14 18:32:29.207: E/AndroidRuntime(7354): at cse.usf.edu.android.Yelp.search(Yelp.java:58)
10-14 18:32:29.207: E/AndroidRuntime(7354): at cse.usf.edu.android.HelloWorldActivity.onCreate(HelloWorldActivity.java:22)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.Activity.performCreate(Activity.java:4531)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
10-14 18:32:29.207: E/AndroidRuntime(7354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
10-14 18:32:29.207: E/AndroidRuntime(7354): ... 11 more
10-14 18:32:36.965: E/AndroidRuntime(7439): FATAL EXCEPTION: main
10-14 18:32:36.965: E/AndroidRuntime(7439): java.lang.RuntimeException: Unable to start activity ComponentInfo{cse.usf.edu.android/cse.usf.edu.android.HelloWorldActivity}: android.os.NetworkOnMainThreadException
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.ActivityThread.access$600(ActivityThread.java:139)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.os.Looper.loop(Looper.java:154)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.ActivityThread.main(ActivityThread.java:4945)
10-14 18:32:36.965: E/AndroidRuntime(7439): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 18:32:36.965: E/AndroidRuntime(7439): at java.lang.reflect.Method.invoke(Method.java:511)
10-14 18:32:36.965: E/AndroidRuntime(7439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-14 18:32:36.965: E/AndroidRuntime(7439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-14 18:32:36.965: E/AndroidRuntime(7439): at dalvik.system.NativeStart.main(Native Method)
10-14 18:32:36.965: E/AndroidRuntime(7439): Caused by: android.os.NetworkOnMainThreadException
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
10-14 18:32:36.965: E/AndroidRuntime(7439): at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
10-14 18:32:36.965: E/AndroidRuntime(7439): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
10-14 18:32:36.965: E/AndroidRuntime(7439): at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-14 18:32:36.965: E/AndroidRuntime(7439): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-14 18:32:36.965: E/AndroidRuntime(7439): at org.scribe.model.Response.<init>(Response.java:27)
10-14 18:32:36.965: E/AndroidRuntime(7439): at org.scribe.model.Request.doSend(Request.java:110)
10-14 18:32:36.965: E/AndroidRuntime(7439): at org.scribe.model.Request.send(Request.java:62)
10-14 18:32:36.965: E/AndroidRuntime(7439): at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12)
10-14 18:32:36.965: E/AndroidRuntime(7439): at cse.usf.edu.android.Yelp.search(Yelp.java:58)
10-14 18:32:36.965: E/AndroidRuntime(7439): at cse.usf.edu.android.HelloWorldActivity.onCreate(HelloWorldActivity.java:22)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.Activity.performCreate(Activity.java:4531)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
10-14 18:32:36.965: E/AndroidRuntime(7439): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
10-14 18:32:36.965: E/AndroidRuntime(7439): ... 11 more
I have done the following:
1. Registered for Yelp API
2. Downloaded scribe 1.3.0, and added it to the libs
folder
3. Added two classes Yelp.java and YelpApi2.java to the src
folder
4. Removed the main
function from Yelp API and I have added the Yelp call to my main activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
// Update tokens here from Yelp developers site, Manage API access.
String consumerKey = "secret";
String consumerSecret = "secret";
String token = "secret";
String tokenSecret = "secret";
Yelp yelp = new Yelp(consumerKey, consumerSecret, token, tokenSecret);
String response = yelp.search("burritos", 30.361471, -87.164326);
}
(Of course, were secret
is my actual information from Yelp)
I am rather new to Android, so I have a feeling it is something simple, but I can't quite figure out the issue, as I have followed Yelp's examples and this post exactly.
回答1:
The name of the exception is pretty clear: You're trying to access the network from the main (UI) thread. The UI thread should be reserved for handling UI events to ensure that your app doesn't stop responding, and you should perform other tasks (like networking or calculations) on a background thread.
来源:https://stackoverflow.com/questions/19370474/yelp-api-android-integration