2

Here I am trying to send json data to my webserver and using php I would like to parse the json and insert the data into mysql databse. I am getting an error saying Value stdClass of type java.lang.String cannot be converted to JSONObject.

How can I resolve this error?

   private void insertToDb() throws JSONException {
    billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
            .getText().toString();
    String invNumber = textViewInvNo.getText().toString();
    String bcode = barCode.getText().toString();
    String description = itemDesc.getText().toString();
    String wt = weightLine.getText().toString();
    String rateAmt = rateAmount.getText().toString();
    String making = makingAmount.getText().toString();
    String netr = netRate.getText().toString();
    String iTotal=itemtotal.getText().toString();
    String vatAmt =textViewVat.getText().toString();
    String sumAmt =textViewSum.getText().toString();
    String crtDate =textViewCurrentDate.getText().toString();
    try {
        jsonObject.put("custInfo", custSelected.toString());
        jsonObject.put("invoiceNo", invNumber);
        jsonObject.put("barcode", bcode);
        jsonObject.put("desc", description);
        jsonObject.put("weight",wt );
        jsonObject.put("rate", rateAmt);
        jsonObject.put("makingAmt", making);
        jsonObject.put("net_rate",netr);
        jsonObject.put("itemTotal",iTotal );
        jsonObject.put("vat", vatAmt);
        jsonObject.put("sum_total", sumAmt);
        jsonObject.put("bill_type", billType);
        jsonObject.put("date", crtDate);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        itemSelectedJson.put(index, jsonObject);
        index++;
    } catch (JSONException e) {
        e.printStackTrace();
    }
   String jsonarray = itemSelectedJson.toString().trim();
    JSONObject jb= new JSONObject();
    jb.put("selected",itemSelectedJson);

 Map<String,Object> params = new HashMap<>();
    params.put("Array",jsonarray);


    final JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, INVEST_URL, new JSONObject(params), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("RESPONSE", response.toString());

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("JSONERROR",error.toString());
        }
    });



    final RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(objectRequest);

}

I am new to programming so any help is appreciated.Thank you:)

This is the stack trace:

12-01 17:17:50.840 16785-16785/org.bordetuts.com.goldmine D/dalvikvm: GC_FOR_ALLOC freed 345K, 9% free 8597K/9396K, paused 14ms, total 14ms
12-01 17:17:50.841 16785-16785/org.bordetuts.com.goldmine D/SELECTED_ITEMS: [{"custInfo":"Ujwal  9975022560","rate":"24000","weight":"21.00000","desc":"GENTS ANGTHI 22k NO STONE","makingAmt":"200","sum_total":"RS.52094.46","vat":"RS.1021.46","itemTotal":"51073","barcode":"BQSP78BB","net_rate":"24200","date":"2015-12-01","invoiceNo":"1","bill_type":"Invoice"}]
12-01 17:17:50.897 16785-16785/org.bordetuts.com.goldmine W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-01 17:17:50.897 16785-16785/org.bordetuts.com.goldmine W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
12-01 17:17:51.259 16785-16785/org.bordetuts.com.goldmine D/JSONERROR: com.android.volley.ParseError: org.json.JSONException: Value stdClass of type java.lang.String cannot be converted to JSONObject
12-01 17:21:47.657 16785-16792/org.bordetuts.com.goldmine D/dalvikvm: GC_FOR_ALLOC freed 536K, 8% free 8689K/9396K, paused 171ms, total 190ms

enter image description here This is what I see when I debug it.The problem is when i say JSONObject(params) it says params: size=1 objectRequest:"[]

Community
  • 1
  • 1

3 Answers3

2

in your code i think you are trying to convert string stdClass to json object so if you are doing this process then follow below code i am giving an example to convert string to json object

String json = {"phonetype":"N95","cat":"WP"};

try {

    JSONObject obj = new JSONObject(json);

    Log.d("My App", obj.toString());

} catch (Throwable t) {
    Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
}
vishal jangid
  • 2,967
  • 16
  • 22
  • my json array is something like [{"custInfo":"Ujwal 9975022560","rate":"24000","weight":"21.00000","desc":"GENTS ANGTHI 22k NO STONE","makingAmt":"200","sum_total":"RS.52094.46","vat":"RS.1021.46","itemTotal":"51073","barcode":"BQSP78BB","net_rate":"24200","date":"2015-12-01","invoiceNo":"1","bill_type":"Invoice"}] –  Dec 01 '15 at 12:36
  • http://jsonviewer.stack.hu/ open this link and past your json array.. when i past this json array its not valid json array error shows. – vishal jangid Dec 01 '15 at 12:39
  • 1
    you have problem in JsonObjectRequest class because above code working fine. so please provide the code of this class. – vishal jangid Dec 01 '15 at 13:21
1

If what you're following is my exmaple, then please notice that I use a

Map<String,Object> 

The params are inserted like this:

Map<String, Object> params = new ArrayMap<>();
// Adding parameters to request
params.put("email", email); // here email is already a string,
params.put("password", password.toString()); // here password needs the conversion 

Then I pass it straight into a request using a new anonymous JSONObject:

// Creating a JSON Object request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), ...

That way the casting goes through the Object class.

Also note that my syntax creates and puts these params outside and before the request.

EDIT: the reason you see that the size is one, is because it is actually 1, this part:

Map<String,Object> params = new HashMap<>();
params.put("Array",jsonarray);

makes the map size 1, since you put only one object inside - the amount of key-value mappings is the value given for the map size, meaning - the number of keys that point to a value (and the value itself can be another Map, it would still only count as one), see This Documentation for the full explanation on the Map Collection.

About the other part - I suggest trying to add the values you want to the Map and sending that inside the new JSONObject, I answered something else that might help you with this Here. Then - on the php side - print out what you get (to see that you do get it), once you get the params on the php side - you can start wrapping your head around parsing it.

Hope this helps in any way :)

Community
  • 1
  • 1
TommySM
  • 3,793
  • 3
  • 24
  • 36
  • please check the updated question.My problem is when i debug new JSONObject(params) says params:size=1 and an empty objectRequest[]. –  Dec 01 '15 at 14:15
1

size is 1 that is correct because you put in your map that way:

params.put("Array",jsonarray);

so the json would be 1 object {"Array":[...]} and you will have to access this object first before you can parse the array.

meda
  • 45,103
  • 14
  • 92
  • 122
  • If you check the image you will see that the jsonarray is being put into params(Map) then why new JsonObject(params) results in objectRequest:"[] i.e why is it blank.I think I am getting the exception because it is blank.Can you give a code example on how to insert multiple values into a mysql database using volley. It would be very helpful for beginners like me Thank you :) –  Dec 01 '15 at 17:18