1

I have code to Post data in server , this is my code

List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("phone", mPhoneNumber));
                    params.add(new BasicNameValuePair("prod_title", nama));
                    params.add(new BasicNameValuePair("prod_desc", des));

                    JSONObject json = jsonParser.makeHttpRequest(Constants.url_create_product, "POST", params);


                    // check log cat from response
                    Log.d("Create Response", json.toString());
                    try {
                       int success = json.getInt(Constants.TAG_SUCCESS);

                        if (success == 1) {
                            // successfully created user

                            Intent i = new Intent(getApplicationContext(), shoop3Activity.class);
                            i.putExtra("phone", mPhoneNumber);                      
                            startActivity(i);
                                // closing this screen
                            finish();
                        } else {
                            // failed to create user
                            Toast.makeText(getApplicationContext(),"Gagal bikin user", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }    
                    return null;
                }         

but it show error like this

10-08 12:36:11.857: INFO/ActivityManager(59): Displayed activity shoop3.android.edu/.Activity3: 1074 ms (total 1074 ms)
10-08 12:36:16.037: ERROR/JSON Parser(881): Error parsing data org.json.JSONException: Value <div of type java.lang.String cannot be converted to JSONObject
10-08 12:36:16.037: DEBUG/Create Response(881): {"status":1}
10-08 12:36:16.127: INFO/ActivityManager(59): Starting activity: Intent { cmp=shoop3.android.edu/.setupstoreActivity (has extras) }
10-08 12:36:17.507: INFO/ActivityManager(59): Displayed activity shoop3.android.edu/.setupstoreActivity: 1191 ms (total 1191 ms)

any solution pleasee?? it show error parsing data ... string cannot be converted to JSonObject

3 Answers 3

3

The server is responding with HTML data and you are trying to parse it as JSON data. This won't work. You should check what exactly are the parameters you're passing to the server and test this in a browser. Perhaps you need to be logged in/authenticated but you aren't passing the cookies up or something?

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

1 Comment

yes, it happened to me, I was passing wrong value to one of my parameters. I hardly figured out this.
0

This happens because you might have missed any JSon tags or mentioned JSonArray as JSonObject or vice versa. Double check that again and let know if the problem still exists

1 Comment

it solved, i must use code multipart-form data to json , but thanks all , u really help me :))
0

In my app I am getting this error because of the server response. It was returning success but with added unnecessary information:

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined offset: 2</p>
<p>Filename: dingdong_loop/Abc.php</p>
<p>Line Number: 559</p>


<p>Backtrace:</p>
<p style="margin-left:10px">
File: /********************/********/*******/******/*****/******/*****/jaabong/dondon/Api.php<br />
Line: 559<br />
Function: dingdong_loop_handler </p>

<p style="margin-left:10px">
File: /********************/********/*******/******/*****/******/*****/mycontroller.php<br />
Line: 793<br />
Function: call_dingdon_loop_firay   </p>

<p style="margin-left:10px">
File: //********************/********/*******/******/*****/******/*****/ /sample.php<br />
Line: 315<br />
Function: dingdong_loop_once    </p>


</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined offset: 1</p>
<p>Filename: dingdong_loop/Api.php</p>
<p>Line Number: 559</p>


<p>Backtrace:</p>
<p style="margin-left:10px">
File: /********************/********/*******/******/*****/******/*****/Api.php<br />
Line: 559<br />
Function: dingdong_loop_handler </p>

<p style="margin-left:10px">
File: /********************/********/*******/******/*****/******/*****/ /sleep.php<br />
Line: 793<br />
Function: call_dingdong_loop_array  </p>

<p style="margin-left:10px">
File: //********************/********/*******/******/*****/******/*****/wakeup.php<br />
Line: 315<br />
Function: dingdong_loop_once    </p>


</div>{"isSuccess":true,"message":"Inserted successfully.","data":[]}

I managed to get server response as String and then I was parsing it to JSONObject.

I contacted the webservice team and they fixed it.

Sometimes it's the other person's fault.

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.