Reputation: 655
I'm trying to send an API request to Yelp from my Android application. I got the sample code to send a request in Java from here https://github.com/Yelp/yelp-api/tree/master/v2/java
It worked when I ran the script provided in the repository, however, when I added the code to my Android application, I got an error.
Here's my Android app code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YelpAPI yelp = new YelpAPI(
getString(R.string.yelp_consumer_key),
getString(R.string.yelp_consumer_secret),
getString(R.string.yelp_token),
getString(R.string.yelp_token_secret));
String result = yelp.searchForBusinessesByLocation("bar", "San Jose, CA");
System.out.println(result);
}
and the stack trace:
--------- beginning of crash
07-23 12:50:23.848 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: PID: 2328
java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144) ...
I found similar problems on the Internet, and I figured it was because I couldn't send the request using the main thread. How do I send a request using a separate thread?
Upvotes: 1
Views: 83
Reputation: 2749
If you just want to create a new thread, you can do it like this:
new Thread(new Runnable() {
public void run() {
YelpAPI yelp = new YelpAPI(
getString(R.string.yelp_consumer_key),
getString(R.string.yelp_consumer_secret),
getString(R.string.yelp_token),
getString(R.string.yelp_token_secret));
String result = yelp.searchForBusinessesByLocation("bar", "San Jose, CA");
System.out.println(result);
}
}).start();
Information on Android threads can be found at: http://developer.android.com/guide/components/processes-and-threads.html
Upvotes: 1