I was trying to get the content from my wordpress website. I wanted the content from the specify post as you can see there: (understanding, this website is not real but it came from real website, I just replace the different title, url, etc.) http://pastebin.com/PWuC8usi
I am using the WordPress API (RESTFUL API).
Error said:
Error parsing data Value <!DOCTYPE of type java.lang.String cannot be
converted to JSONObject
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream inputStream = null;
String result= null;
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
try {
HttpResponse response = client.execute(httpGet);
inputStream = response.getEntity().getContent();
// convert inputstream to string
if(inputStream != null){
result = convertInputStreamToString(inputStream);
Log.i("App", "Data received:" +result);
}
else
result = "Failed to fetch data";
return result;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void parseJSON(String data){
try{
JSONObject jsonResponse = new JSONObject(data);
//JSONArray jsonMainNode = jsonResponse.getJSONArray("posts");
JSONArray jsonMainNode = jsonResponse.getJSONArray("content");
Log.i("App", "jsonMainNode = "+jsonMainNode);
int jsonArrLength = jsonMainNode.length();
Log.i("App", "jsonArrLength = "+jsonArrLength);
for(int i=0; i < jsonArrLength; i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String postTitle = jsonChildNode.getString("title");
String postUrl = jsonChildNode.getString("url");
String postDate = jsonChildNode.getString("date");
String postContent = jsonChildNode.getString("content");
tvPostTitle.setText("Page title: " +postTitle);
tvPostUrl.setText("Page URL: " +postUrl);
tvPostDate.setText("Date: " +postDate);
tvPostContent.setText("Content: " +android.text.Html.fromHtml(postContent).toString());
}
}catch(Exception e){
Log.i("App", "Error parsing data " +e.getMessage());
}
}
The string you are parsing is not in JSON format.
The following snippet:
Will give you the very same error message:
I would guess you are connecting to wrong API endpoint. Check the URL you are using. Connect there using your own web browser.
I would comment but I do not have enough ‘reputation’ so I’ll take a guess.
is the first line of HTML documents. It is likely that you are not parsing a JSON object but instead the webpage itself. It could also be that you’re being redirected to an error page (such as HTTP 404), which would also begin with an
declaration. If you need further assistance, it would be useful to post a log, specifically the ‘data’ String used in parseJSON(String data).