Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am not able to parse String into json Object. I want to set that Sponsor name to the
etSponsor_name
But it shows

<pre>24864-24864/com.example.fortuinelife D/TAG: SponcerIDException: org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
2022-01-13 15:03:18.204 24864-24864/com.example.fortuinelife D/TAG: SponcerID_EX_Response: <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://tempuri.org/">Fortune Life</string>


What I have tried:

<pre>I am trying to set value for sponsorID to the textview but but I'm not able to parse response to line etSponsor_name.setText(strSponsorId);   this is my code

Java
<pre>TextView sponsorName,etSponsor_name;
String strSponsorId = "";
sponsorId.setOnFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View view, boolean b) {
            strSponsorId = sponsorId.getText().toString();
            String url = Config.URL + "webapi/fortuneapi.asmx/CheckSponcer?SponcerID=" + strSponsorId;
            Log.d("TAG", url);
            StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                        Log.d("TAG", "SponcerID: " + response);


                        try {

                            JSONObject jsonObject = new JSONObject(response);
                            strSponsorId = jsonObject.optString("SponsorID","");
                            Log.i("TAG", "parseData: " + strSponsorId);
                            etSponsor_name.setText(strSponsorId);


                        } catch (Exception e) {
                            Log.d("TAG", "SponcerIDException: " + e);
                        }
                        Log.d("TAG", "SponcerID_EX_Response: " + response);

                    }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("TAG", "onErrorResponse_of_sponcerID: " + error.getMessage());


                }
            });
Posted
Updated 13-Jan-22 4:21am
Comments
Richard Deeming 13-Jan-22 6:54am    
The error seems to suggest that the response is XML, not JSON.

1 solution

you are receiving XML as response instead of JSON that is why following line is giving exception.

JSONObject jsonObject = new JSONObject(response);


You need to change these two lines
JSONObject jsonObject = new JSONObject(response);
strSponsorId = jsonObject.optString("SponsorID","");

To
JSONObject jsonObject = XML.toJSONObject(response);
strSponsorId = jsonObject.optString("string","");


Hopefully this should work
 
Share this answer
 
Comments
shweta_5392 15-Jan-22 2:14am    
Cannot resolve XML

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900