0

Am working on android project Upon calling the api its throwing the exception as below. i think everything has done correctly but too long am fed up on this exception.

org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

here my code

public class APIConnectionManager {

    private static int CONNECTION_TIMEOUT = 60000;
    static JSONObject response = null;
    public static final int GET = 1;
    public static final int POST = 2;


    /**
     * Function to place a get call to the customer profile API that returns a JSONObject
     *
     * @param url     - The path to the resource
     * @param params - A list of name value pairs
     * @return    - JSONObject
     */
    public JSONObject doCustomerAPIGet(String url, int method, List<NameValuePair> params) {

        try{

            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // check the http method request type
            if(method==POST){

                HttpPost httpPost = new HttpPost(url);

                // adding post params
                if(params!=null){

                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }

                httpResponse = httpClient.execute(httpPost);
            }else if (method==GET){

                // appending params to the url
                if (params!=null) {
                    String paramString = URLEncodedUtils.format(params, "utf-8");

                    url += "?" + paramString;
                }

                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);
            }

            httpEntity = httpResponse.getEntity();

            String result = EntityUtils.toString(httpEntity);

            response = new JSONObject(result);


        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e){
            e.printStackTrace();
        }

        return response;

    }
}

error pointing at this line

response = new JSONObject(result);

likewise am getting response

{"data":{"customerno":1339451,"loyalty_id":"9700354522","firstname":"raju","lastname":"king","email":"[email protected]","mobile":"9700354522","address":"1st street","city":"chennai","pincode":"600028","links":[]},"status":"success"}

can someone please advise on this.

4
  • consult with your backend developer Commented Aug 18, 2016 at 10:44
  • please see my response above Commented Aug 18, 2016 at 10:46
  • 1
    <!DOCTYPE probably suggests that you're getting HTML instead of JSON. Run in debug mode to verify what response you're getting. Commented Aug 18, 2016 at 10:48
  • i commented after seeing that, there is some error in your api that is causing this issue, this can also occur if you are missing any parameters (though api should handle that with a proper error message probably) Commented Aug 18, 2016 at 10:53

1 Answer 1

1

You are not getting response which can be converted into JSON. There is an error in your API response so just debug your app and show the error to your API developer.

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

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.