代码在Xamarin Android上有效,但在Java(HttpPost JSON)中无效 - java

我开始在Xamarin中进行开发,然后决定许可证的价格可能有点昂贵,因此我将代码转移到Java。

我有一个小块,它使用JSON对象执行POST,它可以在Xamarin中工作,而不能在Java中工作。

Xamarin:

    var client = new HttpClient ();
    var content = new FormUrlEncodedContent(new Dictionary<string, string>() { 
        {"action", "getEpisodeJSON"},
        {"episode", "11813"}

    });
    client.DefaultRequestHeaders.Referrer = new Uri(link);

    var resp = client.PostAsync("http://www.ts.kg/ajax", content).Result;
    var repsStr = resp.Content.ReadAsStringAsync().Result;
    dynamic res = JsonConvert.DeserializeObject (repsStr);

Android:

    HttpClient httpclient = new DefaultHttpClient();

    // 2. make POST request to the given URL
    HttpPost httpPost = new HttpPost("http://www.ts.kg/ajax");

    String json = "";

    // 3. build jsonObject
    JSONObject jsonObject = new JSONObject();
    jsonObject.accumulate("action", "getEpisodeJSON");
    jsonObject.accumulate("episode", "11813");

    // 4. convert JSONObject to JSON to String
    json = jsonObject.toString();

    // 5. set json to StringEntity
    StringEntity se = new StringEntity(json);

    // 6. set httpPost Entity
    httpPost.setEntity(se);

    // 7. Set some headers to inform server about the type of the content
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    httpPost.addHeader("Referer", "http://www.ts.kg");

    // 8. Execute POST request to the given URL
    HttpResponse httpResponse = httpclient.execute(httpPost);

    // 9. receive response as inputStream
    InputStream inputStream = httpResponse.getEntity().getContent();

    // 10. convert inputstream to string
    String result;
    if(inputStream != null)
        result = convertInputStreamToString(inputStream);

在Android中进行此类POST的正确方法是什么?

UPD
当前的问题是我得到一个空的结果字符串。

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

参考方案

我最终通过Fiddle捕获了设备的所有请求(好的教程在这里:http://tech.vg.no/2014/06/04/how-to-monitor-http-traffic-from-your-android-phone-through-fiddler/)

区别在于cookie,因此我使用了HttpContex变量,如下所述:
Android HttpClient Cookie

而且我还有一个不同的Content-Type,所以我手动设置了这个标题:

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

Java Map,如何将UTF-8字符串正确放置到地图? - java

我有一个地图,LinkedHashMap更确切地说。我想在上面放一个字符串对象。然后,我读取此值以查看实际存储的内容。字符串本身具有非ASCII字符(西里尔文,韩文等)。将其放到地图上然后阅读后,这些字符将替换为??? s。一些代码:Map obj = new LinkedHashMap(); System.out.println("name: &…

如何从包含文本的HTML页面转换为JSON - java

假设我在HTML页面中通过TextArea输入了文本。如何使用Eclipse将其转换为JSON形式?我使用TextArea输入的字符串的示例是对于AWSDataTransfer产品,这是公共定价计划。关于跨EC2可用区的数据传输:在所有AWS地区,入站费用均为$ 0.01 / GB。在所有AWS地区,出站费用为$ 0.01 / GB。 参考方案 我想“ Ec…

JSON处理linkedHashMap - java

在我的服务包中,我返回一个linkedHashMap并将其转换为控制器中的JSON,例如:JSONObject jsonObject = new JSONObject(); jsonObject.put("dataObject", JSONObject.fromObject(linkedHashMap)); return jsonObje…

JSON PATH字段NULL检查表达式 - java

我有一个像bellow的json数组:{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sa…

如何删除杰克逊反序列化的未知枚举键? - java

我有一个Map<Alert, Boolean>映射,其中填充了相应的JSON:{ "BUTTON": true, "UNKNOWN": false } 我发现我可以将DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL的Jackson配置为true,…