在我的 Android 应用程序中,我需要连接服务器并调用一些函数 这就是我正在做的事情:
HttpConnector.httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = HttpConnector.httpclient.execute(HttpConnector.httppost);
其中nameValues
是参数列表(可以)。
我收到错误
java.lang.IllegalStateException: Could not execute method of the activity
那么问题出在哪里呢?
最佳答案
您不能在 Activity 中执行此操作。您必须在另一个线程中执行此操作。尝试使用 AsyncTask 或新线程:
Thread thread = new Thread()
{
@Override
public void run() {
try {
// HTTP stuff
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
关于java - HTTP响应错误java.lang.IllegalStateException : Could not execute method of the activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173349/