0

I'm getting floored by this error. I'm retrieving a JSON Object to display in a recyclerview in Android Studio. I've made some changes based on previous answers here, but it seems not to make a difference.

The code receives a JSON Object with a nested JSON array ("data"), containing a list of receipts.

Example JSON

{
  "status": true,
  "message": "Data fetched successfully",
  "data": [
    {
      "id": "2",
      "receipt_id": "2",
      "parent_id": "0",
      "has_children": "0",
      "product_id": "4",
      "product_code": "24904",
      "product_name": "LIGHT SOLAR S/STEEL GLASS LENS COLOUR CHANGE PDQ H25.5CM",
      "quantity_expected": "120",
      "quantity_received": "120",
      "quantity_putaway": "120",
      "quantity_held": "0",
      "reason_held": "",
      "unit_quantity": "0",
      "subinner_uom": "",
      "subinner_quantity": "0",
      "subinner_unit_quantity": "0",
      "subinner_weight": "0.000",
      "subinner_cubic": "0.000000",
      "inner_uom": "",
      "inner_quantity": "0",
      "inner_unit_quantity": "0",
      "inner_weight": "0.000",
      "inner_cubic": "0.000000",
      "outer_expected": "10",
      "outer_uom": "CTN",
      "outer_quantity": "10",
      "outer_unit_quantity": "12",
      "outer_weight": "0.000",
      "outer_cubic": "0.000000",
      "outer_length": "0.000",
      "outer_width": "0.000",
      "outer_height": "0.000",
      "pallet_uom": "",
      "pallet_quantity": "0",
      "pallet_unit_quantity": "0",
      "pallet_weight": "0.000",
      "pallet_cubic": "0.000000",
      "batch_number": " ",
      "date_expiry": "0000-00-00",
      "serial_number": "",
      "length": "36.000",
      "width": "29.000",
      "height": "29.000",
      "cubic": "0.030276",
      "weight": "3.000",
      "barcode": "",
      "cost_current": "0.00",
      "status": "0",
      "is_uniform_pallet": "0",
      "pallet_id": "",
      "is_checked": "1",
      "is_putaway": "1",
      "location_id": "1757",
      "last_location": "10139",
      "split_key": "V09AFONYL7",
      "created_by": "9",
      "modified_by": "6",
      "datetime_created": "2019-08-13 12:11:06",
      "datetime_modified": "2019-08-14 16:58:52"
    },
    {
      "id": "3",
      "receipt_id": "2",
      "parent_id": "0",
      "has_children": "0",
      "product_id": "3",
      "product_code": "24900",
      "product_name": "LIGHT SOLAR FAIRY WH LED PK100",
      "quantity_expected": "1008",
      "quantity_received": "1008",
      "quantity_putaway": "1008",
      "quantity_held": "0",
      "reason_held": "",
      "unit_quantity": "0",
      "subinner_uom": "",
      "subinner_quantity": "0",
      "subinner_unit_quantity": "0",
      "subinner_weight": "0.000",
      "subinner_cubic": "0.000000",
      "inner_uom": "",
      "inner_quantity": "0",
      "inner_unit_quantity": "0",
      "inner_weight": "0.000",
      "inner_cubic": "0.000000",
      "outer_expected": "84",
      "outer_uom": "CTN",
      "outer_quantity": "84",
      "outer_unit_quantity": "12",
      "outer_weight": "0.000",
      "outer_cubic": "0.000000",
      "outer_length": "0.000",
      "outer_width": "0.000",
      "outer_height": "0.000",
      "pallet_uom": "",
      "pallet_quantity": "0",
      "pallet_unit_quantity": "0",
      "pallet_weight": "0.000",
      "pallet_cubic": "0.000000",
      "batch_number": " ",
      "date_expiry": "0000-00-00",
      "serial_number": "",
      "length": "36.000",
      "width": "23.000",
      "height": "23.000",
      "cubic": "0.019044",
      "weight": "3.500",
      "barcode": "",
      "cost_current": "0.00",
      "status": "0",
      "is_uniform_pallet": "0",
      "pallet_id": "",
      "is_checked": "1",
      "is_putaway": "1",
      "location_id": "1512",
      "last_location": "0",
      "split_key": "",
      "created_by": "9",
      "modified_by": "6",
      "datetime_created": "2019-08-13 12:11:06",
      "datetime_modified": "2019-08-14 16:58:52"
    },

Fetch JSON

private void fetchJSON(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ReceiptsLineInterface.JSONURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        ReceiptsLineInterface api = retrofit.create(ReceiptsLineInterface.class);

        Call<String> call = api.getString();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.i("Responsestring", response.body());
                //Toast.makeText()
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        Log.i("onSuccess", response.body());
                        setRecyclerItems(response.body());
                    } else {
                        //Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

Code block with the errors

/**
     * Array list generator that should take JSON array from API query of Job
     * or Item list
     * @return items
     */
    public void setRecyclerItems(String response){

        try {
            //getting the whole json object from the response
            JSONObject obj = new JSONObject(response);

            if(obj.optString("status").equals("true")){
                ArrayList<Receipts> modelRecyclerArrayList = new ArrayList<>();
                JSONArray dataArray  = obj.getJSONArray("data");

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

                    Receipts modelRecycler = new Receipts();
                    JSONObject dataobj = dataArray.getJSONObject(i);

                    modelRecycler.setName(dataobj.getString("product_name"));
                    modelRecycler.setProductCode(dataobj.getString("product_code"));
                    modelRecycler.setReceiptID(dataobj.getString("receipt_id"));

                    modelRecyclerArrayList.add(modelRecycler);

                }

                retrofitAdapter = new RetrofitAdapter(this,modelRecyclerArrayList);
                recyclerView.setAdapter(retrofitAdapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

            }else {
                Toast.makeText(getApplicationContext(), obj.optString("message")+"", Toast.LENGTH_SHORT).show();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

Logcat Response

2019-12-16 14:39:33.498 8562-8562/com.prostock.scanner I/onSuccess: array (
      'status' => true,
      'message' => 'Data fetched successfully',
      'data' => 
      array (
        0 => 
        array (
          'id' => '1',
          'receipt_id' => '1',
          'parent_id' => '0',
          'has_children' => '0',
          'product_id' => '26',
          'product_code' => '57280',
          'product_name' => 'LIGHTS FAIRY CONNECTABLE 400',
          'quantity_expected' => '400',
          'quantity_received' => '400',
          'quantity_putaway' => '400',
          'quantity_held' => '0',
          'reason_held' => '',
          'unit_quantity' => '0',
          'subinner_uom' => '',
          'subinner_quantity' => '0',
          'subinner_unit_quantity' => '0',
          'subinner_weight' => '0.000',
          'subinner_cubic' => '0.000000',
          'inner_uom' => '',
          'inner_quantity' => '0',
          'inner_unit_quantity' => '0',
          'inner_weight' => '0.000',
          'inner_cubic' => '0.000000',
          'outer_expected' => '50',
          'outer_uom' => 'CTN',
          'outer_quantity' => '50',
          'outer_unit_quantity' => '8',
          'outer_weight' => '0.000',
          'outer_cubic' => '0.000000',
          'outer_length' => '0.000',
          'outer_width' => '0.000',
          'outer_height' => '0.000',
          'pallet_uom' => '',
          'pallet_quantity' => '0',
          'pallet_unit_quantity' => '0',
          'pallet_weight' => '0.000',
          'pallet_cubic' => '0.000000',
          'batch_number' => ' ',
          'date_expiry' => '0000-00-00',
          'serial_number' => '',
          'length' => '50.000',
          'width' => '22.000',
          'height' => '36.000',
          'cubic' => '0.039600',
          'weight' => '8.700',
          'barcode' => '',
          'cost_current' => '0.00',
          'status' => '0',
          'is_uniform_pallet' => '1',
          'pallet_id' => '',
          'is_checked' => '1',
          'is_putaway' => '1',
          'location_id' => '1641',
          'last_location' => '1562',
          'split_key' => '',
          'created_by' => '9',
          'modified_by' => '9',
          'datetime_created' => '2019-08-06 12:18:23',
          'datetime_modified' => '2019-08-06 16:30:34',
        ),
      ),
    )
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err: org.json.JSONException: Value array of type java.lang.String cannot be converted to JSONObject
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:163)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:176)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:     at com.prostock.scanner.Activities.ScanSessionActivity.setRecyclerItems(ScanSessionActivity.java:154)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:     at com.prostock.scanner.Activities.ScanSessionActivity$1.onResponse(ScanSessionActivity.java:130)
2019-12-16 14:39:33.499 8562-8562/com.prostock.scanner W/System.err:     at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2019-12-16 14:39:33.500 8562-8562/com.prostock.scanner W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2
  • How are encoding Json in php, It seems the response is no a valid json Commented Dec 16, 2019 at 1:46
  • You can use Gson in parsing json to your custom list/object. github.com/google/gson Commented Dec 16, 2019 at 2:02

2 Answers 2

0

It is most likely that your JSON string response is malformed/contains extra characters that makes it an invalid JSON format. You should double check that you're sending the right UTF encoding from your server. Also, you should consider using a GsonConverter in your retrofit call. It will automatically box your response into your custom Java Object. So your retrofit instance will be replaced with:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ReceiptsLineInterface.JSONURL)
                .addConverterFactory(GsonConverterFactory.create()) // this line changed
                .build();

And the return type of your retrofit call would become: Call<MyCustomObject>

See this article for more details: https://futurestud.io/tutorials/retrofit-2-adding-customizing-the-gson-converter

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for the help everyone, I ended up finding the problem. My adapter was pointing to an extra endpoint. Works all good now!

import retrofit2.Call;
import retrofit2.http.GET;

public interface ReceiptsLineInterface {

    String JSONURL = "http://[censored]/api/putaway/id/{receipts_line}/";

    @GET("WRONG ENDPOINT")
    Call<String> getString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.