2013-01-13 51 views
0

我想提出一個Android應用程序,這是它拋出第一個錯誤:解析錯誤的字符串轉換時的JSONObject

Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

這是我的類函數:

package com.ernest.httppost; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.utils.URLEncodedUtils; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    // function get json from url 
    // by making HTTP POST or GET method 
    public JSONObject makeHttpRequest(String url, String method, 
      List<NameValuePair> params) { 

     // Making HTTP request 
     try { 

      // check for request method 
      if(method == "POST"){ 
       // request method is POST 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      }else if(method == "GET"){ 
       // request method is GET 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 

       HttpResponse httpResponse = httpClient.execute(httpGet); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
      }   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 


     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 

我認爲它在最後的try-catch塊中返回了jObj = new JSONObject(json);處的錯誤。任何想法如何解決這個問題?

+0

Java是一種編程語言。 Eclipse是一個IDE(集成開發環境)。 –

+2

我認爲你正在構造JSONObject的字符串應該是一個有效的json字符串。由於錯誤狀態值'

+0

@Jules你應該寫一個答案 –

回答

2

評論晉升回答:

我覺得你與構建的JSONObject的字符串,應該是一個有效的JSON字符串。由於錯誤狀態的值<br無法轉換我猜你正試圖從HTML字符串構造一個json對象。確保你傳遞了一個格式正確的json字符串。例如:

"{\"name\":\"John Ernest Guadalupe\", \"reputation\":181, \"messages\":[\"msg 1\",\"msg 2\",\"msg 3\"]}" 

像davnicwil說:這個問題很可能是由URL不僅僅返回格式正確的JSON字符串原因。

+0

所以我只需要格式化字符串? –

+0

我在哪裏實施格式?我該怎麼做? –

+0

@John json字符串應該由url指定的頁面生成。你能分享你使用的網址嗎? (或只是輸出,如果它是本地url) –

3

看起來像你的迴應<br不是JSON,所以這個解析錯誤是預期的。

您是否在請求中使用了正確的有效負載正確的URL?嘗試調試以查看您正在請求的內容以及響應是什麼 - 錯誤可能很明顯。

+0

'

+0

我確定該URL是正確的,但我不知道您的有效載荷是什麼意思。如何將它格式化爲JSONobject字符串或其他內容? –

+1

我的意思是請求的參數鍵/值對 - 它是POST請求中的有效內容,或者只是編碼在GET的URL中。你的代碼已經在請求中加入了'params',問題是,它們是否正確?如在,他們會產生你需要的JSON響應? – davnicwil