【问题标题】:Java Reading JSON from http request on php - Bad Request 400Java 从 php 上的 http 请求读取 JSON - 错误请求 400
【发布时间】:2012-05-23 15:14:48
【问题描述】:

我愿意从 http 请求中读取一个简单的 json 文件。 这是我试图阅读的 URL:

http://tccdahora.servehttp.com/teste.php

所以,我找到了一种将 URL 转换为 JSON 的方法,但它不起作用,我尝试了许多不同的方法或在帖子中获取标题,但没有成功。

这是方法:

public static JSONArray getPostJSONObject(String url) throws Throwable {

    // faz o POST na pagina php e obtem o inputstream de resposta
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url); // "http://tccdahora.servehttp.com/teste.php"
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    httppost.setHeader("host", url);
    //httppost.setHeader("Content-Type", "application/json");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    // transforma o que o php printar em uma strigzona
    JSONArray jArray;
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    sb.append(reader.readLine() + "\n");

    String line = "0";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    String result = sb.toString();
    Log.e("", result);
    // transformando em uma array de objetos JSON
    jArray = new JSONArray(result);
    return jArray;
}

我做错了什么吗?我收到了错误的请求错误,帖子中的字符串返回了一个 HTML CODE,通知错误,而不是我的浏览器显示的 json。

非常感谢您的关注!

【问题讨论】:

    标签: java php json


    【解决方案1】:

    删除以下行:

    httppost.setHeader("host", url);
    

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2023-03-11
      • 2015-04-10
      相关资源
      最近更新 更多