2012-05-06 27 views
0

私は以下のコードを使用して(mysqlからデータを取得します)、アンドロイドエミュレータのバージョン4 で問題なく実行しますが、バージョン2.1で同じコード結果がlogcatparssingorg.json.JSONException:型java.lang.Stringの値をJSONArrayに変換できません

に表示され、これがエラーである:

parssing error org.json.JSONException: A JSONArray text must start with '[' at character 1 of <br /> 

2.2 parssingorg.json.JSONExceptionを使用する場合、これは誤りである:タイプjava.langでの値。文字列をJSONArrayに変換できません

 protected Void doInBackground(Void... params) { 
    MealActivity.foodList = new ArrayList<ItemInList>();  

    try 
    { 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair",k)); 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://10.0.2.2/y.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); 
     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()); 
     } 
     //convert response to string 
     try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),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()); 
     } 

     //parsing reesult 

     try{ 
     Log.e("log_tag", " result before parsing " + result); 

     String foodName=""; 
     int Description=0; 

     jArray = new JSONArray(result); 
     JSONObject json_data = null; 

     for (int i = 0; i < jArray.length(); i++) { 
     json_data = jArray.getJSONObject(i); 
     if(json_data!=null) 
      { 
      foodName=json_data.getString("Food"); 
      Description=json_data.getInt("Calories");    
      item.setName(foodName); 
      item.setDescription(Description); 
      item.setSelected(false); 
      MealActivity.foodList.add(item); 
      item=new ItemInList(); 


          } 
         } 
+0

例外を与えるJSONの一部を貼り付けます。私の推測では、故障したジャソンです。 – Warpzit

+0

あなたが持っているものは、json配列ではなく普通のjsonオブジェクトです... – Warpzit

+0

彼らはおそらくjsonの実装を更新しているはずです... GSONを使って試してみてください。 – Warpzit

答えて

0

さて、私は今かなり確信しています。問題は、jsonオブジェクトをjson配列にキャストしようとしていることです。この質問は似ています:"A JSONArray text must start with '[' at character 1 of null"。 あなたがすべきことは単にjsonobjectを代わりに使うことです。

0

次のコードと入力文字列は、2.2で私のために働いていました。しかし、私はあなたの入力文字列をきれいにしなければならなかったので、それはいくつかの制御(見えない)文字を持っています。あなたはそれをチェックしたいかもしれません。

String data = "[{\"Food\":\"\\u062a\\u0641\\u0627\\u062d \\u0628\\u062f\\u0648\\u0646 \\u0627\\u0644\\u0642\\u0634\\u0631\\u0629\",\"Protein\":\"0.27\",\"Water\":\"86.67\",\"Magnesium\":\"4\",\"Phosphorus\":\"11\",\"Sodium\":\"0\",\"Zinc\":\"0.05\",\"Vit_C\":\"4\",\"Fat_Sat\":\"0\",\"Fat_Mono\":\"0\",\"Vit_B12\":\"0\",\"Calcium\":\"5\",\"Calories\":\"48\",\"Fiber\":\"1\",\"Cholesterole\":\"0\",\"Potassium\":\"90\",\"Sugar_Tot\":\"10.1\",\"Iron\":\"0.07\",\"Folic_Acid\":\"0\",\"carbohydrates\":\"13\",\"Vit_K\":\"0.6\"}]"; 
    try { 
     JSONArray ja = new JSONArray(data); 
     Log.d("Sample", "Length:" + ja.length()); 
     Log.d("Sample", "Data:" + ja.get(0)); 
    } catch (Exception e) { 
     Log.e("Sample", "Error", e); 
    } 

出力された:

Length:1 
Data:{"Calories":"48","Calcium":"5","Vit_K":"0.6","Food":"تفاح بدون القشرة","Phosphorus":"11","Potassium":"90","Fat_Mono":"0","Folic_Acid":"0","Cholesterole":"0","Vit_C":"4","Water":"86.67","Vit_B12":"0","Sugar_Tot":"10.1","Sodium":"0","Fat_Sat":"0","Zinc":"0.05","carbohydrates":"13","Magnesium":"4","Iron":"0.07","Fiber":"1","Protein":"0.27"} 
+0

2.2がuesedし、このエラーが表示されます。parssingorg.json.JSONException:java.lang.String型の値をJSONArrayに変換できません。 – user2012

+0

どのように私は文字列beforを配列を整理することができます – user2012

+0

私は送信元の制御文字を知っていると言い、それを修正するか、制御文字が制限されている場合、いくつかの正規表現/単純な文字列を使用します。 – kontinuity

関連する問題