Welcome Guest. Sign in or Signup

0 Answers

JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject in android [on hold]

Asked by: 91 views
Android, FaceBook, Java

Currently i'm working on parsing feeds (images and title) from Facebook page using Graph API. But I'm not able to fetch the data, instead I'm getting error in logcat as02-01 14:38:55.624: W/System.err(801): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 02-01 14:38:55.651: W/System.err(801): at org.json.JSON.typeMismatch(JSON.java:107) 02-01 14:38:55.651: W/System.err(801): at org.json.JSONObject.<init>(JSONObject.java:158) 02-01 14:38:55.662: W/System.err(801): at org.json.JSONObject.<init>(JSONObject.java:171) 02-01 14:38:55.662: W/System.err(801): at info.androidhive.jsonparsing.MainActivity$GetContacts.doInBackground(MainActivity.java:119) 02-01 14:38:55.662: W/System.err(801): at info.androidhive.jsonparsing.MainActivity$GetContacts.doInBackground(MainActivity.java:1) 02-01 14:38:55.690: W/System.err(801): at android.os.AsyncTask$2.call(AsyncTask.java:185) 02-01 14:38:55.701: W/System.err(801): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 02-01 14:38:55.720: W/System.err(801): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 02-01 14:38:55.720: W/System.err(801): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)After research I discovered that this might be due to "space" in the response, but I could not find any space in the response.Response{ "id": "100004832745674", "feed": { "data": [ { "id": "100004832739703_242561485934567", "from": { "name": "xxxxx", "id": "xxxxxx" }, "privacy": { "value": "" }, "type": "status", "created_time": "2014-01-31T08:41:40+0000", "updated_time": "2014-01-31T08:41:40+0000" }, { "id": "1000048327xxx_239736xxxxxx", "from": { "name": "xxxx", "id": "100004xxxxxx" }MainActivity.javapublic class MainActivity extends ListActivity {private ProgressDialog pDialog;// URL to get contacts JSON private static String url = "https://developers.facebook.com/tools/explorer/143xxxxxxx/?method=GET&path=100004xxxxxxxxx%3Fme%2Ffeed";// JSON Node names private static final String TAG_DATA = "data"; private static final String TAG_NAME = "name";// contacts JSONArray JSONArray data = null;// Hashmap for ListView ArrayList<HashMap<String, String>> dataList;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);dataList = new ArrayList<HashMap<String, String>>();ListView lv = getListView();// Listview on item click listener lv.setOnItemClickListener(new OnItemClickListener() {@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // getting values from selected ListItem String name = ((TextView) view.findViewById(R.id.name)) .getText().toString();// Starting single contact activity Intent in = new Intent(getApplicationContext(), SingleContactActivity.class); in.putExtra(TAG_NAME, name);startActivity(in);} });// Calling async task to get json new GetContacts().execute(); }/** * Async task class to get json by making HTTP call * */ private class GetContacts extends AsyncTask<Void, Void, Void> {@Override protected void onPreExecute() { super.onPreExecute(); // Showing progress dialog pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Please wait..."); pDialog.setCancelable(false); pDialog.show(); }@Override protected Void doInBackground(Void... arg0) { // Creating service handler class instance ServiceHandler sh = new ServiceHandler();// Making a request to url and getting response String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr);if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr);// Getting JSON Array node data = jsonObj.getJSONArray(TAG_DATA);// looping through All Contacts for (int i = 0; i < data.length(); i++) { JSONObject c = data.getJSONObject(i); String name = c.getString(TAG_NAME);// tmp hashmap for single contact HashMap<String, String> datac = new HashMap<String, String>();// adding each child node to HashMap key => value datac.put(TAG_NAME, name);// adding contact to contact list dataList.add(datac); } } catch (JSONException e) { e.printStackTrace(); } } else { Log.e("ServiceHandler", "Couldn't get any data from the url"); }return null; }@Override protected void onPostExecute(Void result) { super.onPostExecute(result); // Dismiss the progress dialog if (pDialog.isShowing()) pDialog.dismiss();/** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new SimpleAdapter( MainActivity.this, dataList, R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name });setListAdapter(adapter); } }I know most of them asked this question but none of the solutions helped me. Please help me to solve this

Answer Question