2012-03-27 111 views
0

當我嘗試轉換的HTTP POST響應於JSONArray我得到的錯誤:錯誤轉換HTTP POST響應於JSON

org.json.JSONException:java.lang.String類型的值不能轉化爲JSONArray

錯誤發生在該行:JSONArray jArray = new JSONArray(result);

字符串結果的值是[{「return」:「1」}]但它在開始時包含一個額外的空白字符,當它被移除時,可以解決問題。然而,這個角色並不是空白的,因爲修剪並不能解決問題。我認爲POST響應存在一些問題,可能構建得非常糟糕? (或者POST請求錯誤?)歡迎任何幫助。

GET請求工作得很好,但我需要做POST請求。

這是代碼:

HttpPost("usuarioLogin.php",nameValuePairs); 
String result = ConvertResponseToString(); 
try{ 

    JSONArray jArray = new JSONArray(result); 
    JSONObject json_data=null; 
    for(int i=0;i<jArray.length();i++){ 
     json_data = jArray.getJSONObject(i);     
     ret = json_data.getInt("return"); 
      retorno = (ret==1)?true:false; 
    }    

} 
catch(JSONException e1){   
     e1.printStackTrace();  
} catch (ParseException e1) { 
    e1.printStackTrace(); 
}  

這是函數HttpPost()的代碼

private void HttpPost(String php, ArrayList<NameValuePair> nameValuePairs) 
{ 
      try{ 
        HttpClient httpclient = new DefaultHttpClient(); 
        String host = com.android.taggies.LoginUser.getContext().getResources().getString(R.string.host); 
        HttpPost httppost = new HttpPost("http://"+host+php); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
      }catch(Exception e){ 
        Log.e("log_tag", "Error in http connection "+e.toString()); 
      } 
} 

這是函數ConvertResponseToString()的代碼

private String ConvertResponseToString() 
{ 
    //convert response to string 
    String result = null; 
    try{ 
     //BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     result=sb.toString(); 
    } 
    catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    return result; 
} 

這是我的PHP代碼,回覆郵政

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("dbTaggies"); 

$q=mysql_query("SELECT count(*) as 'return' FROM users 
WHERE name='$_POST[user]' AND password ='$_POST[pass]'"); 

while($e=mysql_fetch_assoc($q)) 
{ 
     $output[]=$e; 
}  

print(json_encode($output)); 

mysql_close(); 
?> 
+0

的[問題初始化一個JSONObject]可能重複(http://stackoverflow.com/questions/6062007/issue-initializing-a-jsonobject) – 2012-03-27 16:46:14

+0

@Cnarf:第一檢查JSON是有效還是無效,通過使用該工具:[ json驗證器](http://jsonlint.com/) – 2012-03-27 17:08:42

+0

正如我在我的第一篇文章中所述,這裏的問題是POST REQUEST和/或RESPONSE,我恰好需要一個嚴重編碼/解碼的字符串用於JSON。但問題是POST REQUEST或RESPONSE,我沒有使用HTTP POST的經驗,並且我犯了一個我不知道的錯誤。 我可以轉換爲JSON字符串「[{」return「:」1「}]沒有問題,但字符串[{」return「:」1「}]我收到作爲一個POST響應是不可能轉換爲JSON,因爲我的HTTP POST在某處存在問題 – Cnarf 2012-03-27 20:28:07

回答

0

的問題就解決了。

PHP文件保存爲UTF-8 WITH BOM,解決方案是將文件保存爲UTF8,沒有BOM,並且POST響應中的首字符被刪除。

1

我用這對我來說一直工作正常:

DefaultHttpClient client = new DefaultHttpClient(); 
    JSONObject json = null; 
    String resoult = ""; 
    try 
    { 
      HttpPost postRequest = new HttpPost("XXXXXXXXXXXXXXXXXX"); 
      HttpResponse postResponse = client.execute(postRequest); 
      HttpEntity postResponseEntity = postResponse.getEntity(); 
      if (postResponseEntity != null) 
        resoult= EntityUtils.toString(postResponseEntity); 
      json = new JSONObject(resoult);    
    }    
    catch(Exception e) 
    { 

    } 
+0

GET請求與POST請求不同 – 2012-03-27 17:04:00

+0

只需將HttpGet更改爲HttpPost – goodm 2012-03-27 17:10:12

+0

它與本代碼的情況相同,它在開頭處包含一個額外的空白字符的字符串結果(value [0])。正如我之前所說,修剪並不能解決問題。它應該是一個HTTP POST的問題...任何幫助? – Cnarf 2012-03-29 10:04:07