Restful API: Unable to get information from WordPress Website

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).

Read More

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());

        }
    }

Related posts

2 comments

  1. The string you are parsing is not in JSON format.

    The following snippet:

    try {
        String data = "<!DOCTYPE not a json data goes here ...";
        JSONObject jsonResponse = new JSONObject(data);
    } catch (JSONException e) {
        Log.d("App", "Error parsing data " + e.getMessage());
    }
    

    Will give you the very same error message:

    Error parsing data Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

    I would guess you are connecting to wrong API endpoint. Check the URL you are using. Connect there using your own web browser.

  2. I would comment but I do not have enough ‘reputation’ so I’ll take a guess.

    <!DOCTYPE
    

    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

    <!DOCTYPE
    

    declaration. If you need further assistance, it would be useful to post a log, specifically the ‘data’ String used in parseJSON(String data).

Comments are closed.