user1906403
user1906403

Reputation: 21

Error Android - received HTML instead of JSON

I'm working on a Android application and I'm trying to get a JSON response from a server which is configured to return a json object (".../current_user.json") when receives a GET message, but the answer I get is in HTML format and not in JSON format as expected.

I don't understand why is this happening because I did the same requests on the browser and with the program RESTClient and got the right answer in JSON format.

Here is the code I'm using.

        JSONObject json = new JSONObject();

            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSoTimeout(params, 10000);
            HttpClient httpClient = new DefaultHttpClient(params);
            HttpGet get = new HttpGet(url_getiduser);

            HttpResponse response = httpClient.execute(get);                 
            String sresponse = "error";

            Log.d("url get", url_getiduser);
            Log.d("pedido get", get.getMethod());
            if(response != null)
            {
                InputStream in = response.getEntity().getContent();
                sresponse = convertStreamToString(in);

                Log.d("resposta http", sresponse);
                if(!sresponse.equals("error"))
                {

                    JSONObject object = new JSONObject(sresponse);
                    id_user = (String) object.get("id");

                    json = object;
                    Log.d("objecto json", object.toString());

                }
                else Log.d("Error on json parser", sresponse);

Upvotes: 2

Views: 1094

Answers (1)

Archie.bpgc
Archie.bpgc

Reputation: 24012

There are few cases where you get HTML text

  1. You might have called a wrong function which gives a 404 page.

  2. Might be a database error on server side where you will get database error message

  3. Server might be sending a styled data which has HTML tags

But you better Log the response and paste it here.

Upvotes: 1

Related Questions