2

ive got a working listview which receives JSON from my DB. But when i try with this link: I get "Error parsing data org.json.JSONException: Value

The JSON output is clean and should work! My activitiy

package com.spxc.ssa.streaming;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.spxc.ssa.streaming.task.JsonAsync;
import com.spxc.ssa.streaming.task.JsonAsync.JsonListener;

public class ListShowsController extends SherlockListActivity implements
        OnClickListener {

    private ProgressDialog mDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.dblist);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Shows");

        JsonAsync asyncTask = new JsonAsync();
        // Using an anonymous interface to listen for objects when task
        // completes.
        asyncTask.setJsonListener(new JsonListener() {
            @Override
            public void onObjectReturn(JSONObject object) {
                handleJsonObject(object);
            }
        });
        // Show progress loader while accessing network, and start async task.
        mDialog = ProgressDialog.show(this, getSupportActionBar().getTitle(),
                getString(R.string.loading), true);
        asyncTask.execute("");

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    private void handleJsonObject(JSONObject object) {
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        try {

            JSONArray shows = object.getJSONArray("items");

            for (int i = 0; i < shows.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = shows.getJSONObject(i);

                map.put("video_id", String.valueOf(i));
                map.put("video_title", "" + e.getString("video_title"));
                //map.put("season", "Season: " + e.getString("season"));
                map.put("video_location", "" + e.getString("video_location"));
                mylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.dbitems,
                new String[] { "video_title", "video_location" }, new int[] { R.id.item_title,
                        R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv
                        .getItemAtPosition(position);

                Intent myIntent = new Intent(ListShowsController.this,
                        ShowsController.class);
                myIntent.putExtra("video_title", o.get("video_title"));
                //myIntent.putExtra("season", o.get("season"));
                myIntent.putExtra("video_location", o.get("video_location"));
                startActivity(myIntent);
            }
        });

        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

}

AsyncTask

package com.spxc.ssa.streaming.task;

import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class JsonAsync extends AsyncTask<String, Void, JSONObject> {

    public interface JsonListener {
        public void onObjectReturn(JSONObject object);
    }

    public void setJsonListener(JsonListener jsonListener) {
        mListener = jsonListener;
    }

    private JsonListener mListener;
    public static final String TAG = JsonAsync.class.getSimpleName();

    @Override
    protected JSONObject doInBackground(String... params) {
        // The argument passed into tasks are arrays. So you can pass 0 or more
        // arguments, when calling from activity, it's really up to you. They
        // just have to be separated by commas. Like this :
        // new JsonAsync.execute("thisUrl", "thatUrl", "anotherUrl");
        // Although I am only grabbing the first item in the array, by calling
        // params[0] below.

        JSONObject object = null;

        if (params != null && params.length > 0) {
            object = JSONfunctions.getJSONfromURL(params[0]);
        } else {
            Log.e(TAG, "Task needs an argument to be able to retrieve data.");
        }

        // I return the item out of this method, because it is happening of the
        // UI thread, so it can;t update items on the screen. You can work with
        // a database from this method. That is actually recommended.
        return object;
    }

    @Override
    protected void onPostExecute(JSONObject result) {
        // This method can touch UI components without throwing an error.
        if (result != null && mListener != null) {
            mListener.onObjectReturn(result);
        }
        super.onPostExecute(result);
    }
}

I've never seen this error before! What is the problem?

Any help is much appreciated ! Thanks

EDIT: My other activity which works:

package com.spxc.ssa.streaming;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.spxc.ssa.streaming.task.JsonAsync;
import com.spxc.ssa.streaming.task.JsonAsync.JsonListener;

public class ListMoviesController extends SherlockListActivity implements
        OnClickListener {

    private ProgressDialog mDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.dblist);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Movies");

        JsonAsync asyncTask = new JsonAsync();
        // Using an anonymous interface to listen for objects when task
        // completes.
        asyncTask.setJsonListener(new JsonListener() {
            @Override
            public void onObjectReturn(JSONObject object) {
                handleJsonObject(object);
            }
        });
        // Show progress loader while accessing network, and start async task.
        mDialog = ProgressDialog.show(this, getSupportActionBar().getTitle(),
                getString(R.string.loading), true);
        asyncTask.execute("http://strongpixel.com/java/movies.php");

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    private void handleJsonObject(JSONObject object) {
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        try {

            JSONArray shows = object.getJSONArray("items");

            for (int i = 0; i < shows.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = shows.getJSONObject(i);

                map.put("id", String.valueOf(i));
                map.put("name", "" + e.getString("name"));
                //map.put("date", "Released: " + e.getString("date"));
                map.put("path", "" + e.getString("path"));
                mylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.dbitems,
                new String[] { "name", "path" }, new int[] { R.id.item_title,
                        R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv
                        .getItemAtPosition(position);

                Intent myIntent = new Intent(ListMoviesController.this,
                        MoviesController.class);
                myIntent.putExtra("name", o.get("name"));
                myIntent.putExtra("season", o.get("season"));
                myIntent.putExtra("path", o.get("path"));
                startActivity(myIntent);
            }
        });

        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

}

Logcat:

03-12 08:35:18.722: E/log_tag(25208): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
03-12 09:06:52.877: V/CustomViewAbove(26397): Received ACTION_DOWN
03-12 09:06:52.920: V/CustomViewAbove(26397): onInterceptTouch moved to:(30.26232, 662.0), diff:(29.26232, 1.0), mLastMotionX:1.0
03-12 09:06:52.936: V/CustomViewAbove(26397): onInterceptTouch moved to:(73.95948, 663.08905), diff:(72.95948, 0.08905029), mLastMotionX:1.0
03-12 09:06:52.940: V/CustomViewAbove(26397): this slide allowed true dx: 72.95948
03-12 09:06:52.940: V/CustomViewAbove(26397): Starting drag! from onInterceptTouch
03-12 09:06:52.993: V/SlidingMenu(26397): changing layerType. hardware? true
03-12 09:06:53.229: V/SlidingMenu(26397): changing layerType. hardware? false
03-12 09:06:54.214: V/CustomViewAbove(26397): Received ACTION_DOWN
03-12 09:06:54.450: D/dalvikvm(26397): GC_CONCURRENT freed 73K, 7% free 4495K/4812K, paused 3ms+6ms, total 24ms
03-12 09:06:55.025: E/log_tag(26397): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

Edit: I tried the same JSON, but just in a txt file strongpixel.com/java/JSON.txt, this works without any problem, it takes some time to load, but it works.

7
  • Try it with a fraction of the JSON, to test if your set-up does indeed work. Might be that the JSON just takes to long to load. Otherwise, also post your logcat. Commented Mar 12, 2013 at 8:15
  • Hi, it do work, i added logcat in the question post Commented Mar 12, 2013 at 8:20
  • Well, you're trying to create an object out of something that is a String. Not sure where it happens exactly. Try debugging. Could be this line; object = JSONfunctions.getJSONfromURL(params[0]); Commented Mar 12, 2013 at 8:31
  • It's looks like you can't download proper data from link. I would recommend you to set up proxy thought Fiddler and use it on your device or just look at server response in debug mode, so you can check if you get proper JSON result. I bet you receive some HTTP error like 403. Commented Mar 12, 2013 at 8:57
  • This is really weird since i used the same exact JSON strongpixel.com/java/JSON.txt and this works without any problem ... Commented Mar 12, 2013 at 8:58

2 Answers 2

1

You can't get it work cause your code can't load proper JSON String. There is only one place where you make download:

JSONfunctions.getJSONfromURL(params[0])

You get the obvious error: JSON object can't be created from string like <!doctype html>. It means that you download HTML data instead of JSON. For simple test you can try next code and you will get the same error:

JSONObject object = new JSONObject("<!DOCTYPE html>");

You can get this wrong response from numerous reasons: wrong request, your fire wall, proxy, even server might return wrong result if you don't specify User-Agent header or doesn't perform authorization. I do recommend using some web debug tools that will help you to handle such situations and make sure that your app receive valid data. If you are using Windows i would recommend you to use Fiddler, it is free. Here is example how you can setup fiddler for your test device: http://www.cantoni.org/2011/06/28/debug-http-android-fiddler Anyway there is alternative tools for other operating system.

Sign up to request clarification or add additional context in comments.

Comments

0

I got it working! It was a simple and idiotic mistake. The server i was connecting to didnt allow POST so in the adapter i changed Httppost to Httpget

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.