0

I want to send an JSONObject using retrofit 2 to server and i am sending this kind of json object :

{"Order Summary":
"[ 
   {
     \ "ProductName\":\"Wine\",
      \"ProductPrice\":\"500\",
      \"ProductQuantity\":\"2\",
      \"ProductCost\":\"1000\",
      \"SellerId\":\"2\"

   },
   {
      \"ProductName\":\"Whiskey\",
      \"ProductPrice\":\"1000\",
      \"ProductQuantity\":\"1\",
      \"ProductCost\":\"1000\",
      \"SellerId\":\"1\"

   }
]"}

due to which i'm unable to parse the json object

and this is the source code iam using :-

private void loadCart()
    {


        Cursor cursor = dbHelper.getCarProducts();
        cursor.moveToFirst();
        do {

            JSONObject product = new JSONObject();
            try {
                product.put("Sellerid",cursor.getString(cursor.getColumnIndex("_Sellerid")));
                product.put("ProductCost",cursor.getString(cursor.getColumnIndex("_Cost")));
                product.put("ProductQuantity",cursor.getString(cursor.getColumnIndex("_Quantity")));
                product.put("ProductPrice",cursor.getString(cursor.getColumnIndex("_Price")));
                product.put("ProductName",cursor.getString(cursor.getColumnIndex("_Name")));
                userCart.put(product);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }while(cursor.moveToNext());
             Cart = new JSONObject();
          try
          {
              Cart.put("OrderSummary",userCart.toString());
          }
          catch (Exception ex)
          {}}

could someone tell me how to rectify this error ?

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
atul kumar
  • 55
  • 6

1 Answers1

2

Here is your mistake

 Cart.put("OrderSummary", userCart.toString());

You get pure JSON Array but why are you converting it to String?

Use,

 Cart.put("OrderSummary", userCart);  // remove .toString()

Edit

By checking your server side code, I think the problem is in index.php file (I'm not expert in PHP)

$requestedData = $response->getBody();

Instead of $response you should use $request object. In order to fix that refer this StackOverflow thread or refer this official doc of Slim Framework.

And to send JSON response from Slim Framework to refer this StackOverflow thread.

Note: While declaring Java variables/objects try to respect Java varibales/method naming conventions. Instead of Cart use cart, this eliminates ambiguity.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • i have tired both case and in both cases i'm getting the same output,so could please give me an viable solution – atul kumar Oct 01 '17 at 14:18
  • Did you remove `.toString()` method. My solution should work. As per your final JSON, the problem is `.toString()` method. After removing that just rebuild the project and retry. And can you elaborate `due to which i'm unable to parse the json object `? Where do you want to parse it? – Shashanth Oct 01 '17 at 14:28
  • yup i have removed the to string and tried sending the data to the server ,but it didn't help me – atul kumar Oct 01 '17 at 14:31
  • Any errors/logs in your Android app? And which technology you're using for the server PHP, JAVA, C#, or other? Is the request hitting server successfully from Android? – Shashanth Oct 01 '17 at 14:35
  • i'm using php on the server and yes it is sucessfully hitting the server as it is giving an error back – atul kumar Oct 01 '17 at 14:41
  • and i'm getting the following error:-errorgoogle.gson.JsonSyntaxException:java.lang.illegalStateException:Expected BEGIN_OBJECT but was STRING at line 1 column1 path$ – atul kumar Oct 01 '17 at 14:42
  • Looks like your client expects JSON Object as a response but the server responding with String or the error is occurring while preparing the request body. Can you edit your question and post the Retrofit part and the request sending code? – Shashanth Oct 01 '17 at 14:54
  • https://github.com/atul92cs/Sqlite4 this is my project on github could you please help me out after seeing it here – atul kumar Oct 01 '17 at 15:07