我正在尝试与适用于 Android 的 Yelp API 集成。
每当我尝试连接时,我都会遇到致命错误。
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
我做了以下事情:
1.注册Yelp API
2.下载scribe 1.3.0,并将其添加到libs
文件夹中
3.在文件夹中添加了两个类Yelp.java和YelpApi2.javasrc
4.从 Yelp API中删除了该main
功能,并将 Yelp 调用添加到我的主要活动中。
@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);
}
(当然,secret
是我来自 Yelp 的实际信息)
我对 Android 比较陌生,所以我觉得这很简单,但我不太清楚这个问题,因为我完全按照 Yelp 的例子和这篇文章。