私は以下のコードを使用して(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();
}
}
例外を与えるJSONの一部を貼り付けます。私の推測では、故障したジャソンです。 – Warpzit
あなたが持っているものは、json配列ではなく普通のjsonオブジェクトです... – Warpzit
彼らはおそらくjsonの実装を更新しているはずです... GSONを使って試してみてください。 – Warpzit