【问题标题】:Android HttpClient.execute not workingAndroid HttpClient.execute 不起作用
【发布时间】:2015-07-23 20:09:07
【问题描述】:

我有一个 AsyncTask 如下:

@Override
protected ArrayList<String> doInBackground(String... params) {
    ArrayList<String> result=new ArrayList<>();
    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Constants.URL);
        // Add your data
        List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(
                2);
        nameValuePairs.add(new BasicNameValuePair("id",params[0]));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        HttpEntity httpEntity = response.getEntity();
        inputStream = httpEntity.getContent();

        // deal with response here
        ....


    } catch (Exception e) {
        Log.e("Debug", e + " " + e.getMessage());
    }

    return result;
}

但是,在 Jellybean 上,应用程序运行一段时间后,代码只运行到 httpclient.execute(httppost) 并停在那里。

谁能帮我解决这个问题?

【问题讨论】:

  • LogCat 上显示了一些消息?
  • 你在哪里调用这个 AsyncTask 并且你在调用 postExecute 方法? doInBackground() 中的 UI 没有任何反应

标签: android http-post httpclient


【解决方案1】:

试试这个:

1.检查您的网络连接和服务器可用性。

2.检查您是否已将互联网权限添加到清单中。

<manifest xlmns:android...>
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>

如果我没有误解“在特定时间之后”,那么您的意思是它永远不会成功并且总是停在那里。这可能是因为权限或您的网络连接,因为请求似乎没有返回。

如果它有时运行但有时运行,我认为这一定是由于您的服务器端。

我推荐你使用https://github.com/matessoftwaresolutions/AndroidHttpRestService

我保证我将使用 Android Studio 文件夹结构推送一个新的提交。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2011-09-11
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多