2013-07-23 104 views
2

嗨我在2.3中遇到了Android JSON解析器的問題,在4.0>中一切正常。我試圖把「測試」放在我的所有JSON字段中,但問題仍然存在。org.json.JSONException:無法將類型java.lang.String的值轉換爲JSONObject

這裏是我的代碼:

URL url = new URL(c.getString(R.string.url_ws) + url_ws); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setDoOutput(true); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestMethod("POST"); 
OutputStreamWriter request = new OutputStreamWriter(connection.getOutputStream()); 
request.write("&test=test"); 
request.flush(); 
request.close(); 


String line; 
InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
BufferedReader reader = new BufferedReader(isr); 
StringBuilder sb = new StringBuilder(); 

while ((line = reader.readLine()) != null){ 
    sb.append(line + "\n"); 
} 
String json = sb.toString().trim(); 
try { 
    JSONObject obj = new JSONObject(json); 
} catch (JSONException e) { 
    e.printStackTrace(); 
    Log.d(Tools.TAG+"/debug JSONException", e.toString()); 
    return null; 
} 

這裏是我的例外

debug JSONException(4184): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject 

我看到「價值」之間的雙重空間「的」,所以我想這個問題是一個空字符串,但我不明白。

謝謝。

編輯

這是我JSON一個編碼錯誤,

char a = json.substring(0, 1).charAt(0); 
int ascii = (int)a; 
Tools.myLog(">"+ascii+"<"); 

我發現65279字符

SOLUTION

Why is &#65279; appearing in my HTML?

編碼UTF-8出BOM。

+0

http://stackoverflow.com/questions/17792779/what-is-the-error-表示功能於androidjson誤差的解析數據-ORG-JSON-jsonexcep/17793283#17793283。類似!。發佈你的json。 – Raghunandan

+0

JSONArray obj = new JSONArray(json);使用這個而不是JSONObject obj = new JSONObject(json); – Giant

+0

我的JSON是不是數組: { 「bHasError」:假的, 「sSessionId」: 「5e10be14c408732179aa4731899882fd」, 「iMembreId」:7510064 「iCurrentDate」:1374565720 .... –

回答

1
while ((line = reader.readLine()) != null){ 
    sb.append(line + "\n"); 
} 

而是試圖

while ((line = reader.readLine()) != null){ 
    sb.append(line); 
    json = sb.toString().substring(0, sb.toString().length()-1); 
} 

按照此方法用於獲取JSON對象:

public JSONObject getJSONObjFromUrl(String url, List<NameValuePair> params) {  
      System.out.println("url:: "+url); 
      System.out.println("params:: "+ params +" " +params.get(0)); 
      // Making HTTP request 
      try { 
       // 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(); 

      } 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); 
       } 
       is.close(); 

       json = sb.toString().substring(0, sb.toString().length()-1); 
       // Log.e("JSON:: ", json); 
      } 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; 

     } 
+0

我刪除了\ n,但你的詭計與子字符串發生錯誤:debug JSONException(4461):org.json.JSONException:未終止的對象在字符242的 –

+2

去到站點http://json.parser.online.fr/ 。檢查你的JSON是否正確 –

+0

尼斯非常好的網站,我發現它是我的JSON中的[「」]之間的空間的錯誤,我更新我的問題。 TYVM! –

相關問題