android - 没有得到响应response = httpclient.execute(request);

标签 android httpclient http-post

public class HTTPPoster {
    public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
    {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);

        HttpEntity entity;
        StringEntity s = new StringEntity(c.toString());
        s.setContentEncoding((Header) new BasicHeader(HTTP.DEFAULT_CONTENT_CHARSET, "application/json"));
        entity = s;
        request.setEntity(entity);

        HttpResponse response;

        response = httpclient.execute(request);

        return response;
    }

}

这是代码,但 response = http.client.execute(request) 没有得到响应。我找不到原因。

最佳答案

您应该异步调用该方法。然后它将使用相同的代码。 将这两行代码添加到您的项目中

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

和 将最低sdk版本设置为9

关于android - 没有得到响应response = httpclient.execute(request);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939122/

相关文章:

java - 来自 Gradle 的 Android 中的 JUnit 测试 : "package android.test does not exist"

java - Android - 指纹扫描仪在 5 次尝试后停止工作?

java - 正确使用ReSTLet ClientResource

java - 从效率角度来看,使用 HttpClient 发布文件的最佳方式

java - 在单个连接中发送多个 POST 请求的 HttpUrlConnection

android - 使用okhttp上传图片到服务器

android - android中无法为多个按钮调用多个onclick事件

c# - 给httpclient添加参数

java - 使用HttpServletResponse.setStatus后如何获取状态

android - 我的 BottomNavigationBar 图标超出了屏幕