有几天我一直试图在Android Studio上找出这个多线程的东西。我过去只使用过SwingWorker,在尝试了几种方法让我的https连接启动主线程之后我就把头发拉出来了。我已经尝试了从使用ASyncTask到每次建立连接时启动一个线程的所有内容,但似乎没有任何工作。我的应用每次都会崩溃。最近的尝试如下图所示。
编辑:这不是重复。我已经读过那个帖子,它没有做任何事情来帮助解决这个问题。我正在尝试在主线程之外执行此任务,但我不知道为什么我的代码没有这样做,因为我在run()中更改为后台线程;甚至在以防万一的情况下开始一个新线程。
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);
}
}
错误日志:
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)
答案 0 :(得分:1)
公共类YelpActivity扩展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);
}
}
注意:在Activity中实现Runnable不是一个好习惯 类。像我在run方法中使用的那样使用Core Java线程或使用其他 Android背景机制,如AsyncTask / IntentService。