0

For a few days I've been trying to figure out this multithreading thing on Android Studio. I've only used SwingWorker in the past and after trying several ways to get my https connection to initiate off of the main thread I'm pulling my hair out. I've tried everything from using ASyncTask to just starting a thread everytime the connection gets made, but nothing seems to work. My app crashes every time. The most recent attempt is pictured below.

Edit: It's not a duplicate. I've read that thread and it doesn't do anything to help solve this problem. I'm attempting to execute this task outside of the main thread, but I don't know why my code isn't doing that already since I change to the background thread in run(); and even start a new thread just in case.

public class YelpActivity extends AppCompatActivity implements Runnable{
        private yelpLoader load;

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



    }
    public void run(){
        Thread t = new Thread();
        t.start();
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        YelpAPI test = new YelpAPI();
        test.run();

    }

      public void yelpClick(View v){
          run();

        changeYelpResults(v,"test");


    }


    private void changeYelpResults(View v, String text){

        TextView t = (TextView) findViewById(R.id.ResturauntID);
        t.setText(text);

    }



}

Error Log:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: tastr.tastr, PID: 3560
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5198)
                      at android.view.View$PerformClick.run(View.java:21147)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      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: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5198) 
                      at android.view.View$PerformClick.run(View.java:21147) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      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:132)
                      at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
                      at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
                      at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
                      at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
                      at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5198) 
                      at android.view.View$PerformClick.run(View.java:21147) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      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:132) 
                      at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89) 
                      at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146) 
                      at tastr.tastr.YelpAPI.run(YelpAPI.java:190) 
                      at tastr.tastr.YelpActivity.run(YelpActivity.java:26) 
                      at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5198) 
                      at android.view.View$PerformClick.run(View.java:21147) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      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) 
Remixt
  • 597
  • 6
  • 28
  • 1
    Possible duplicate of [NetworkOnMainThreadException](http://stackoverflow.com/questions/5150637/networkonmainthreadexception) – Onik Sep 25 '16 at 00:48
  • It's not a duplicate. I've read that thread and it doesn't do anything to help solve this problem. I'm attempting to execute this task outside of the main thread, but I don't know why my code isn't doing that already since I change to the background thread in run(); and even start a new thread just in case. – Remixt Sep 25 '16 at 00:51
  • I don't know what `YelpAPI.run()` does, but if in the method you don't start a new thread (and the stacktrace tells me that I'm right) the question is a duplicate. You perform the network operation on the main thread, hence the exception. – Onik Sep 25 '16 at 00:57
  • It does start a new thread though. Are you saying that if I start a new thread and then in that thread make a call to another class then that class will execute that call on the main thread anyway? – Remixt Sep 25 '16 at 01:06
  • 1
    _"I'm attempting to execute this task outside of the main thread"_ No, you aren't. You still call `Activity`'s `run()` method on UI thread. _"but I don't know why my code isn't doing that already since I change to the background thread in run()"_ No, you don't. It's still being executed on UI thread. _"and even start a new thread just in case"_ Yes, you do, but without passing a `Runnable` object in the `Thread`'s constructor it does nothing. [Reference](https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html) – Onik Sep 25 '16 at 01:15

1 Answers1

1

public class YelpActivity extends AppCompatActivity{ private yelpLoader load;

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



} 
public void run(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                YelpAPI test = new YelpAPI();
                test.run();        
            }
        }).start();
    }
  public void yelpClick(View v){
      run();

    changeYelpResults(v,"test");


}


private void changeYelpResults(View v, String text){

    TextView t = (TextView) findViewById(R.id.ResturauntID);
    t.setText(text);

}

}

Note: It's not a good practice to implement Runnable in Activity class. Use Core Java threads like I did in the run method or Use other Android Background mechanism like AsyncTask/IntentService.

EEJ
  • 678
  • 5
  • 12
  • Thanks very much! Just as you posted this I managed to get AyncTask to work, even though its the exact same code I tried yesterday. Now its working for no apparent reason. – Remixt Sep 25 '16 at 01:42
  • Sound nice but Async Task also have a major flaw which is mentioned here https://developer.android.com/guide/components/processes-and-threads.html So if you are falling under the caution of AsyncTask then better use Core Java Threads by defining any Executors Or use IntentService. – EEJ Sep 25 '16 at 01:46