本文介绍了Yelp的API Android的整合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与Android上Yelp的API相集成。

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 注册Yelp的API

1. Registered for Yelp API

2 下载隶1.3.0 和将其添加到文件夹

2. Downloaded scribe 1.3.0, and added it to the libs folder

3。增加了两个班的和 YelpApi2.java 来的的src 文件夹

3. Added two classes Yelp.java and YelpApi2.java to the src folder

4。删除了Yelp的API的功能,我已经加入了Yelp的调用我的主要活动。

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

}

(当然,是秘密是我从Yelp的实际信息)

(Of course, were secret is my actual information from Yelp)

我是相当新至Android,让我有一种感觉它是简单的东西,但我不能完全弄清楚这个问题,因为我跟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.

推荐答案

该异常的名称是pretty的明确:你试图访问从主(UI)线程的网络。在UI线程应预留用于处理UI事件,以确保您的应用程序不会停止响应,您应该执行其他任务(如网络或计算)在后台线程。

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.

这篇关于Yelp的API Android的整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 19:35