74436

Error Android - received HTML instead of JSON

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

I don't understand why is this happening because I did the same requests on the browser and with the program <strong>RESTClient</strong> and got the right answer in <strong>JSON</strong> 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);

Answer1:

There are few cases where you get <strong>HTML text</strong>

<ol> <li>

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

</li> <li>

Might be a database error on server side where you will get <strong>database error</strong> message

</li> <li>

Server might be sending a styled data which has <strong>HTML tags</strong>

</li> </ol>

But you better Log the response and paste it here.

Recommend

  • Android: Check network and real internet connectivity
  • How to properly load heavy collection?
  • How to pass params to POST method in angular 2
  • 200kb image to base64 cannot send to web service
  • calling rest web service?
  • How can I get my ServiceStack Tests to authenticate using RestSharp?
  • get headers from get Http request in Angular
  • RestSharp requests on momentapp's restful api
  • Set Specific Color to the Region Occupied by LineChart
  • Java Parser Json as array multidimensional
  • Get max ID and min ID from specific column in MySQL php
  • Android Login- checking if username and password match with MySQL database
  • Generating random numbers directly inside a .htaccess file
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • android Navigation Bar hiding and persantage of usable screen overlap
  • File extension of zlib zipped html page?
  • Dart - Isolate Cross Window Communication
  • Repository Browser Only - \"Repository moved permanently to… please relocate”
  • How can I restyle a word when rendering a pdf with pdf.js?
  • what makes a request a new request in asp.net C#
  • Memory error in python- how to use more memory
  • Differences in dis-assembled C code of GCC and Borland?
  • System.InvalidCastException: Specified cast is not valid
  • Rails Find when some params will be blank
  • Read a local file using javascript
  • Algorithm for a smudge tool?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • How to make Safari send if-modified-since header?
  • Web-crawler for facebook in python
  • How do I rollback to a specific git commit
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • java string with new operator and a literal