java - HTTP响应错误java.lang.IllegalStateException : Could not execute method of the activity

标签 java android

在我的 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/

相关文章:

java - MP3播放器,播放键没有声音

java - 在 Glassfish 3.1.2 上使用 WebService 客户端代码部署 Ear 时出现 AbstractMethodError

android - 带有 AlarmManager 的 IntentService

java - 如何在 jfreechart、netbeans 中调整此图表的大小?

java - Sendmail.php 网络表单发送空白电子邮件

java - KeyStore Explorer - 创建 key 对?

android - ionic POST 请求错误,net::ERR_CONNECTION_REFUSED

java - 此类语句不支持使用executeUpdate(String)方法

android - 将 ImageView 添加到 linearlayout 会抛出目标异常

android - 如何创建垂直的 BottomNavigationView android?