I'm trying to parse and get the values from a URL
with JSON
format. The format of the JSON
when the URL
is used in a browser is shown below:
{"CompanyID":"1","Message":"Not active user","Success":"false","UserID":"2"}
My JsonParser
code is shown below:
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* Created by Fathi on 6/16/2015.
*/
public class JsonParser {
final String TAG = "JsonParser.java";
static InputStream is = null;
static JSONObject jObj = null;
static JSONArray jArray = null;
static String result = "";
public JSONArray getJSONFromUrl(String url) {
// make HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpget);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
Log.e(TAG, "JSON status code: " + statusCode);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
result = sb.toString();
Log.e(TAG, "JSON string: " + result);
} catch (Exception e) {
Log.e(TAG, "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
// return JSON String
return jArray;
}
}
The code I'm using in my activity is as follows:
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "**MY URL COMES HERE**";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONArray jsonArray = jParser.getJSONFromUrl(yourJsonStringUrl);
// loop through all users
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
// Storing each json item in variable
String Success = c.getString("Success");
// show the values in our logcat
Log.e(TAG, "Success: " + Success);
} } catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {}
My Logcat is giving me the following error (I have the HTTP response and the string):
06-16 03:50:10.841 2470-2489/iss.voltappinterface E/JsonParser.java﹕ JSON status code: 200
06-16 03:50:10.911 2470-2489/iss.voltappinterface E/JsonParser.java﹕ JSON string: <html><head><title>Object moved</title></head><body>n<h2>Object moved to <a href="/voln
06-16 03:50:10.921 2470-2489/iss.voltappinterface E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value <html><head><title>Object of type java.lang.String cannot be converted to JSONObject
I can see that my JSON
is being processed as HTML
for some reason, and that is what I don't understand, seeing as it looks perfect in the browser. Any help would much appreciated.
Look at
content-type
header on your endpoint, it's probablytext/html
and should beapplication/json
Edit : As @Fathi Eid suggest, you can force the Content-type:
Note that is a fix and you should really consider changing the response content type on the api server and not in the client.