简体   繁体   English

Android中用于内容类型的JSON对象的HTTP POST请求问题

[英]Problems with HTTP POST request for JSON object for content-type in Android

The following is my code for a HTTP POST request in an Android device running Android 4.1.2. 以下是我针对运行Android 4.1.2的Android设备中的HTTP POST请求的代码。 I have question: 我有问题:

  • This code works perfectly on a Samsung device whereas it is giving "Content-type is not application/json" on an HTC device. 该代码在Samsung设备上完美运行,而在HTC设备上给出“ Content-type not application / json”。

    public static String POST(String url, JSONObject obj) { Log.i("JSONPOSTBEGIN", "Beginning of JSON POST"); 公共静态字符串POST(String url,JSONObject obj){Log.i(“ JSONPOSTBEGIN”,“ JSON POST的开始”); InputStream inputStream = null; InputStream inputStream = null; String result = ""; 字符串结果=“”;

     HttpClient httpclient = new DefaultHttpClient(); try { HttpPost post = new HttpPost(url); post.setHeader("Content-type", "application/json"); post.setHeader("Accept", "application/json"); StringEntity se = new StringEntity(obj.toString()); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); HttpResponse httpResponse = mHhttpclient.execute(post); // receive response as inputStream inputStream = httpResponse.getEntity().getContent(); // convert inputstream to string if(inputStream != null) { result = convertInputStreamToString(inputStream); } else result = "Did not work!"; } catch (Exception e) { Log.d("InputStream", e.getLocalizedMessage()); } Log.i("JSONPOSTEND", "End of JSON data post methos..."); return result; 

    } }

For it to work on HTC and all devices, you must do this 为了使其能够在HTC和所有设备上运行,您必须执行此操作

private static HttpEntity getHttpEntity(String stringEntity) {
    HttpEntity entity = null;
    try {
        entity = new StringEntity(stringEntity, HTTP.UTF_8);
        ((StringEntity) entity).setContentType("application/json;charset=UTF-8");
        ((StringEntity) entity).setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));

    } catch (Exception e) {
        Log.d("error creating entity: " + stringEntity);
    }
    return entity;
}

Note that headers are likely being set by this 请注意,标题可能是由此设置的

new HttpPost(url); 新的HttpPost(url);

and that before you add new headers like for "content-type" you may have to call remove header (Content-type) first.... 在添加新的标题(如“ content-type”)之前,您可能必须先调用remove header(Content-type)。

you need logging to figure it all out.for more on that see accepted ans here 您需要记录以解决所有问题。有关更多信息,请参见此处接受的答案。

and get WIRE & HEADER logging up in your test environment 并在测试环境中记录WIRE&HEADER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用内容类型 x-www-form-urlencoded android 改造发送帖子请求 - How to send post request with content-type x-www-form-urlencoded android retrofit 为什么发生okhttp的Post方法错误[不支持HTTP请求中指定的Content-Type头:application / x-www-form-urlencoded]? - Why Post method of okhttp occur error [Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded]? 带有身份验证的Android Java http删除Content-Type - Android java http with authentication remove Content-Type Android Http请求POST JSON - Android Http Request POST JSON 如何在Android中发送带有参数和内容类型的HttpDelete请求 - How to Send HttpDelete Request with paramete and content-type in android 正文中的HTTP请求POST JSON对象 - HTTP Request POST JSON Object in Body Spring-更改内容类型HTTP标头 - Spring - changing Content-type HTTP header 从Android发送JSON HTTP POST请求 - Sending a JSON HTTP POST request from Android Android:如何使用自定义http标头和内容类型创建xml-rpc请求 - Android: how to create post xml-rpc request with custom http header & content type '如何使用带有“Content-Type”的 Volley 发送 Post 请求:“application/x-www-form-urlencoded” - 'How to send Post request using Volley with “Content-Type”:“application/x-www-form-urlencoded”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM