2012-05-23 125 views
0

我愿意从http请求中读取简单的json文件。 这是URL IM tryng阅读:Java从http请求的PHP读取JSON - 错误的请求400

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

所以,我已经有了一个方法将URL转换成JSON,但其没有工作,香港专业教育学院tryed许多不同的方式或刚开上后一个头,但是没有成功。

这是方法:

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。

非常感谢您的关注!

回答

0

删除以下行:

httppost.setHeader("host", url);