【问题标题】:Code works in Xamarin Android but doestn't work in Java (HttpPost JSON)代码适用于 Xamarin Android,但不适用于 Java (HttpPost JSON)
【发布时间】:2015-01-16 17:11:05
【问题描述】:

我开始在 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);

安卓:

    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;

    }

【问题讨论】:

  • 什么问题?你能发布你的堆栈跟踪来确定问题吗?
  • 结果字符串为空。但是当我启动 Xamarin 应用程序时 - 结果字符串不是空的,因为它应该是。
  • 我还注意到代码执行速度非常快,就好像根本没有完成任何网络请求一样。 (我ping很高,能感觉到web请求的延迟)
  • 你确定没有抛出异常吗?
  • @tyczj,在 result = convertInputStreamToString(inputStream);

标签: java android xamarin.ios xamarin xamarin-studio


【解决方案1】:

我最终通过 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");

【讨论】:

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