2012-05-23 154 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);