4

WebサービスをプルしてJSONデータを解析しようとすると、JSON例外が発生します。

私はこの例外に関連する質問を見てきましたが、それでも解決策を思い付くことができません。

これはWebサービスのJson配列です

[
   {
      "created_at":"2013-03-18T21:30:19Z",
      "id":1,
      "name":"latte",
      "price":"5",
      "updated_at":"2013-03-18T21:30:19Z"
   },
   {
      "created_at":"2013-03-18T21:30:41Z",
      "id":11,
      "name":"black",
      "price":"2",
      "updated_at":"2013-03-18T21:30:41Z"
   },
   {
      "created_at":"2013-03-19T09:38:31Z",
      "id":21,
      "name":"Tea",
      "price":"2",
      "updated_at":"2013-03-19T09:39:02Z"
   }
]

これは、パーサーをバックグラウンドタスクとして使用するクラスの関連部分です。

public class SpinnerDemo extends Activity {

    //JSON node names
    private static final String TAG_DATA = "data";
    private static final String TAG_CREATED_AT = "created_at";
    private static final String TAG_ID_DRINK = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_PRICE = "price";
    private static final String TAG_UPDATED_AT = "updated_at";
    private static final String MAP_API_URL = "http://notTheActualURL.com/drinks.json";
    private BackGroundTask bgt;

    Spinner drinkField;

    ArrayList<Drink> drinkList = new ArrayList<Drink>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_main);
        buildDrinkDropDown();
    }

    private void buildDrinkDropDown() {

        List<NameValuePair> apiParams = new ArrayList<NameValuePair>(1);
        apiParams.add(new BasicNameValuePair("call", "drinkList"));

        bgt = new BackGroundTask(MAP_API_URL, "GET", apiParams);

        try {
            JSONObject drinkJSON = bgt.execute().get();
            // Getting Array of drinks
            JSONArray drinks = drinkJSON.getJSONArray(TAG_DATA);

            // looping through All drinks
            for (int i = 0; i < drinks.length(); i++) {

                JSONObject d = drinks.getJSONObject(i);

                // Storing each json item in variable

                String id = d.getString(TAG_ID_DRINK);
                String createdAt = d.getString(TAG_CREATED_AT);
                String updatedAt = d.getString(TAG_UPDATED_AT);
                String price = d.getString(TAG_PRICE);
                String name = d.getString(TAG_NAME);



                // add drink
                drinkList.add(new Drink( createdAt ,id, name, price,updatedAt ));
            }

これは、解析を行う実際のクラスです

package com.android.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
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.os.AsyncTask;
import android.util.Log;

public class BackGroundTask extends AsyncTask<String, String, JSONObject> {

     List<NameValuePair> postparams = new ArrayList<NameValuePair>();
     String URL = null;
     String method = null;

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

     public BackGroundTask(String url, String method, List<NameValuePair> params) {
      this.URL = url;
      this.postparams = params;
      this.method = method;
     }

     @Override
     protected JSONObject doInBackground(String... params) {
      // TODO Auto-generated method stub
      // Making HTTP request
      try {
       // Making HTTP request
       // check for request method

       if (method.equals("POST")) {
        // request method is POST
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);
        httpPost.setEntity(new UrlEncodedFormEntity(postparams));

        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(postparams, "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, "utf-8"), 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 TEST " + e.toString());
      }

      // return JSON String
      return jObj;

     }
    }

これはlogcatからの例外です

03-26 11:32:49.637: E/JSON Parser(714): Error parsing data TEST org.json.JSONException: Value [{"id":1,"created_at":"2013-03-18T21:30:19Z","updated_at":"2013-03-18T21:30:19Z","price":"5","name":"latte"},{"id":11,"created_at":"2013-03-18T21:30:41Z","updated_at":"2013-03-18T21:30:41Z","price":"2","name":"black"},{"id":21,"created_at":"2013-03-19T09:38:31Z","updated_at":"2013-03-19T09:39:02Z","price":"2","name":"Tea"}] of type org.json.JSONArray cannot be converted to JSONObject

そして、これは前の例外の結果だと思いますが、spinnerDemoクラスのnullポイント例外を示す別のエラー

03-26 11:32:49.707: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.main/com.android.main.SpinnerDemo}: java.lang.NullPointerException

これに関するアドバイスや助けをいただければ幸いです

4

3 に答える 3

6

ログのように:

org.json.JSONArrayをJSONObjectに変換できません

JSONArray代わりにルート要素として取得しているがJSONObject、それをJSONObjectに変換しようとしているためです。現在、次のjson構造を取得しています:

[     //<<<<<<<<< JSONArray
   {      //<<<<<<<<< JSONObject

   },
  ....
]

json文字列を変換してそこからJSONArray抽出する必要がありますJSONObject

JSONArray drinkJSON = bgt.execute().get();

doInBackgroundまた、メソッドの戻りタイプをに変更する必要がありますJSONArray

于 2013-03-26T11:53:59.027 に答える
1
        HttpResponse responseGet = client.execute(get);
        HttpEntity resEntityGet = responseGet.getEntity();

        if (resEntityGet != null) {
            String response = EntityUtils.toString(resEntityGet);
        }

この行を使用して、最初に配列を取得します

JSONArray valarray = new JSONArray(response);

次にループしてオブジェクトの内部に入ります

for (int i = 0; i < valarray.length(); i++) {

    JSONObject arr = valarray.getJSONObject(i); 

}

これは私にとって完璧に機能します。

于 2013-03-26T11:56:52.613 に答える
0

リターンjsonはですjsonarrayが、uは取得できますjsonobject

だから、私たちは使うことができます、

 JSONArray drinkJSON = bgt.execute().get();

BackGroundTaskクラスでは 、doInBackgroundメソッド

protected JSONArray doInBackground(String... params) {

また、オブジェクト作成の変更、

     JsonArray jobj;
try {
       jObj = new JSONArray(json);
      } catch (JSONException e) {
       Log.e("JSON Parser", "Error parsing data TEST " + e.toString());
      }
于 2013-03-26T11:54:30.330 に答える