0

I'm attempting to make a call to the Yelp API in android. I am using ASyncTaskLoader but I'm still not able to connect on the emulator. I've never used the task loader before, so I'm sure there's some fundamental thing I'm missing, but I've been at this for several days now and I can't seem to figure it out.

I haven't implemented anything that changes the ui yet as I'm just trying to get the darn API connection working.

public class YelpActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yelp);

        yelpLoader load = new yelpLoader(getApplicationContext());
        load.loadInBackground();

    }

}

Here is my task loader.

class yelpLoader extends AsyncTaskLoader {

    static YelpAPI test = new YelpAPI();
   static String CONSUMER_KEY = "";
   static String CONSUMER_SECRET = "";
   static String TOKEN = "";
   static String TOKEN_SECRET = "";

    public yelpLoader(Context context) {

        super(context);

    }
    public  String loadInBackground() {
        String result = "";
        YelpAPI.YelpAPICLI yelpApiCli = new YelpAPI.YelpAPICLI();
        new JCommander(yelpApiCli);

        YelpAPI yelpApi = new YelpAPI(CONSUMER_KEY, CONSUMER_SECRET, TOKEN, TOKEN_SECRET);
        test.queryAPI(yelpApi, yelpApiCli);

        return null;
    }
}

Error Log:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: tastr.tastr, PID: 4087
                  java.lang.ExceptionInInitializerError
                      at tastr.tastr.YelpActivity.onCreate(YelpActivity.java:44)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
                      at org.scribe.model.Request.send(Request.java:70)
                      at org.scribe.model.Request.send(Request.java:76)
                      at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:129)
                      at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:87)
                      at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:143)
                      at tastr.tastr.YelpAPI.<init>(YelpAPI.java:189)
                      at tastr.tastr.FooLoader.<clinit>(FooLoader.java:11)
                      at tastr.tastr.YelpActivity.onCreate(YelpActivity.java:44) 
                      at android.app.Activity.performCreate(Activity.java:6237) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                   Caused by: android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                      at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
                      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                      at java.net.InetAddress.getAllByName(InetAddress.java:215)
                      at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
                      at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
                      at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
                      at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
                      at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
                      at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
                      at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
                      at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
                      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
                      at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
                      at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
                      at org.scribe.model.Response.<init>(Response.java:29)
                      at org.scribe.model.Request.doSend(Request.java:117)
                      at org.scribe.model.Request.send(Request.java:66)
                      at org.scribe.model.Request.send(Request.java:76) 
                      at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:129) 
                      at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:87) 
                      at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:143) 
                      at tastr.tastr.YelpAPI.<init>(YelpAPI.java:189) 
                      at tastr.tastr.FooLoader.<clinit>(FooLoader.java:11) 
                      at tastr.tastr.YelpActivity.onCreate(YelpActivity.java:44) 
                      at android.app.Activity.performCreate(Activity.java:6237) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.
Remixt
  • 597
  • 6
  • 28
  • What is the problem / question? What does / doesn't work here? You call `loadInBackground` explicitly, so that method should be firing. – OneCricketeer Sep 24 '16 at 01:38
  • The connection never happens and the app crashes. Sorry I'll edit the post to include my errors. – Remixt Sep 24 '16 at 01:39
  • `Caused by: android.os.NetworkOnMainThreadException` should be a red flag. Obviously calling the Loader from the Activity's UI thread won't work, anyways. I might suggest sticking to an AsyncTask, since from what I see in the documentation for AsyncTaskLoader looks like a lot of work just for an API call. https://developer.android.com/reference/android/content/AsyncTaskLoader.html... Does `YelpAPI` not perform its operations in a background thread? – OneCricketeer Sep 24 '16 at 01:43
  • 1
    Oh, nevermind. `YelpAPI` looks like a class you wrote. If you are dealing with a lot of network requests, maybe look into some libraries like Retrofit, OkHTTP, or Volley – OneCricketeer Sep 24 '16 at 01:45
  • I am using AsyncTask already I thought. What I thought this was doing was starting a new background thread to make a call to the yelp api. I tried implementing my working version of the yelp api source code, but that code doesn't work in android because of the same issue, but it works fine in eclipse. – Remixt Sep 24 '16 at 01:46
  • From what I understand, Loaders are to constantly pull data from an API, which, probably is what you may want at some point, but I would recommend seeing if you can at least connect to the API with a standard AsyncTask – OneCricketeer Sep 24 '16 at 01:51

0 Answers0