Errror to parse ej json obtained by consuming web services "com.android.volley.ParseError:"

1

When consuming web services I received a json and it is generating a parse error

Error en la peticion: com.android.volley.ParseError: org.json.JSONException: Value [{"id":1,"name":"Test","description":"some description","price":3.1416,"finish":"some finish","color":"some great color","suitability":"where tu use it","status":true,"picture":null}

This is the code:

 /****** This activity is used for show Tiles ******/
public class CatalogActivity extends Activity {

    /******* Variable It is used. to indicate how many fields are shown  ******/
    private GridLayoutManager mLayout;
    private static TileAdapter mTileAdapter;


    /****** Context to use the Strings ******/
    private static Context mContext;

    /****** Instances of gson, bus, realm, requestque, It is used to connect with web service ******/
    private static Gson mGson;
    private static RequestQueue mRequestQueue;
    private static Bus mBus;
    private static Realm mRealm;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalog);

        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder().build();
        Realm.setDefaultConfiguration(config);
        mRealm = Realm.getDefaultInstance();

        mGson = new Gson();
        mRequestQueue = Volley.newRequestQueue(this);
        mBus = new Bus();

        RecyclerView MyRecyclerView = (RecyclerView) findViewById(R.id.recycler_tile);
        mLayout = new GridLayoutManager(CatalogActivity.this,4);
        MyRecyclerView.setLayoutManager(mLayout);
        MyRecyclerView.setItemAnimator(new DefaultItemAnimator());
        loadTiles();
        List<TileModel> realmResults = CatalogActivity
                .getRealm()
                .where(TileModel.class)
                .findAll();
        mTileAdapter = new TileAdapter(realmResults, CatalogActivity.this);
        MyRecyclerView.setAdapter(mTileAdapter);

    }

    /****** Helpers to connect with web service ******/
    public static Gson getGson() {
        return mGson;
    }

    public static RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    public static Bus getBus() {
        return mBus;
    }

    public static Realm getRealm() {
        return mRealm;
    }

    /****** This methos is used for load Tiles ******/
    public static void loadTiles() {

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
                JSONKeys.KEY_URL_ALL_TILES, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.e("Response", response.toString());
                try {
                    JSONArray mJsonArray = response.getJSONArray(JSONKeys.KEY_NAME);
                    for (int i = 0; i < mJsonArray.length(); i++) {
                        JSONObject mJsonObject = mJsonArray.getJSONObject(i);
                        TileModel mTileModel = CatalogActivity.getGson()
                                .fromJson(mJsonObject.toString(),
                                TileModel.class);
                        CatalogActivity.getRealm().beginTransaction();
                        CatalogActivity.getRealm().copyToRealmOrUpdate(mTileModel);
                        CatalogActivity.getRealm().commitTransaction();
                        Log.e("Agregando Pisos", mTileModel.getName());
                    }
                    CatalogActivity.getBus().post(new SuccessLoadTilesEvent(mContext
                            .getString(R.string.message_aggregate_tiles)));
                } catch (JSONException e) {
                    Log.e("Error al parcear", e.toString());
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Error en la peticion", error.toString());
            }
        });
        CatalogActivity.getRequestQueue().add(jsonObjectRequest);
    }
    }
    
asked by Exbaby 21.07.2017 в 20:45
source

2 answers

0

As you said, you are receiving a JSONArray.

Then the onResponse should be like this:

public void onResponse(JSONArray jsonarray) {
....

Then, the way to parse it would be this:

for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String name = jsonobject.getString("name");
    String description = jsonobject.getString("description");
    ...
}
  

Note: If you are using GSON, the way of parsing it could be different, the utility of this library is to retrieve your answers and link them to Clases that represent the complete object of the response.

    
answered by 21.07.2017 в 22:44
0

The problem is in the result of the request, you try to parsing an incorrect json structure to be a JsonArray , so that it is correct you should have a ] at the end:

[{
    "id": 1,
    "name": "Test",
    "description": "some description",
    "price": 3.1416,
    "finish": "some finish",
    "color": "some great color",
    "suitability": "where tu use it",
    "status": true,
    "picture": null
}]

If this is true, then only convert to JSONArray correctly

   ...
   ...
   @Override
   public void onResponse(JSONObject response) {
     Log.e("Response", response.toString());
     try {
      // *** incorrecto porque no tienes definido un id para el array.
      // JSONArray mJsonArray = response.getJSONArray(JSONKeys.KEY_NAME);

         JSONArray mJsonArray = new JSONArray(response);
      ...
      ...
    
answered by 21.07.2017 в 22:40