1

I am trying to parse a JSON and it shows that error.I tested my code using another JSON url and it working fine.Below are my code details,

URL-JSON URL

MainActivity.java

public class Home extends Activity {
    // url to make request
    private static String url = "http://ads.mocean.mobi/ad?site=38206&zone=156431&type=2&size_x=57&size_y=57&count=5&ua=Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20AppleWebKit/537.22%20(KHTML,%20like%20Gecko)%20Chrome/25.0.1364.152%20Safari/537.22%20117.223.16.66&jsvar=test&type=2&key=6";

    // JSON Node names
    private static final String APP_ARRAYNAME = "var test ";
    private static final String APP_URL = "url";
    private static final String APP_NAME = "text";
    private static final String APP_IMAGE = "img";
    private static final String IMAGE_TYPE = "type";
    private static final String APP_TRACK = "track";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.home);  
        if (Utils.isNetworkAvailable(Home.this)) {
            new MyTask().execute(url);
        } else {
            showToast("No Network Connection!!!");
        }
    }

    class MyTask extends AsyncTask<String, Void, String> {
        ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Home.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) { 
            return Utils.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }
            if (null == result || result.length() == 0) {
                showToast("No data found from web!!!");
                //Home.this.finish();
            } else {
                try {
                    JSONObject mainJson = new JSONObject(result);
                    //JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
                    JSONArray jsonArray = mainJson.getJSONArray(APP_ARRAYNAME);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                        // System.out.println(c.getInt(ID));
                          System.out.println(c.getString(APP_URL));
                          System.out.println(c.getString(APP_NAME));
                          System.out.println(c.getString(APP_IMAGE));
                        //  System.out.println(c.getInt(AGE));
                          System.out.println(c.getString(IMAGE_TYPE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void showToast(String msg) {
        Toast.makeText(Home.this, msg, Toast.LENGTH_LONG).show();
    }
}

Utils.java

public class Utils {
    public static String getJSONString(String url) {
        String jsonString = null;
        HttpURLConnection linkConnection = null;
        try {
            URL linkurl = new URL(url);
            linkConnection = (HttpURLConnection) linkurl.openConnection();
            int responseCode = linkConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream linkinStream = linkConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int j = 0;
                while ((j = linkinStream.read()) != -1) {
                    baos.write(j);
                }
                byte[] data = baos.toByteArray();
                jsonString = new String(data);  
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (linkConnection != null) {
                linkConnection.disconnect();
            }
        }
        Log.i("JSON Response----", ""+jsonString);
        return jsonString;
    }

    public static boolean isNetworkAvailable(Activity activity) {
        ConnectivityManager connectivity = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

}

Please suggest me some solution for parsing this kind of JSON.

NOTE-: This Json is well tested in iPhone and working fine.

Logcat details with JSON Reponse

  04-24 12:09:55.270: I/JSON Response----(2385): var test = [{"url" : "http://ads.mocean.mobi/2/redir/c5c61601-aca9-11e2-8400-a0369f167751/0/306865","text" : "Instagram","img" : "http://img.ads.mocean.mobi/img/50e/cbf/01b1baf/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61601-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61602-aca9-11e2-8400-a0369f167751/0/306834","text" : "Drawsomething","img" : "http://img.ads.mocean.mobi/img/50e/cbe/2f70a73/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61602-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61604-aca9-11e2-8400-a0369f167751/0/306518","text" : "Google Maps","img" : "http://img.ads.mocean.mobi/img/50e/c99/b95edc9/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61604-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61605-aca9-11e2-8400-a0369f167751/0/306765","text" : "Find Friends","img" : "http://img.ads.mocean.mobi/img/50e/cb5/ce11bc6/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61605-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c63d10-aca9-11e2-8400-a0369f167751/0/306733","text" : "Flashlight","img" : "http://img.ads.mocean.mobi/img/50e/cb3/c34c71a/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c63d10-aca9-11e2-8400-a0369f167751"}];
    04-24 12:09:55.290: W/System.err(2385): org.json.JSONException: Value var of type java.lang.String cannot be converted to JSONObject
    04-24 12:09:55.290: W/System.err(2385):     at org.json.JSON.typeMismatch(JSON.java:111)
    04-24 12:09:55.290: W/System.err(2385):     at org.json.JSONObject.<init>(JSONObject.java:158)
    04-24 12:09:55.301: W/System.err(2385):     at org.json.JSONObject.<init>(JSONObject.java:171)
    04-24 12:09:55.301: W/System.err(2385):     at com.impressol.appjiva.Home$MyTask.onPostExecute(Home.java:153)
    04-24 12:09:55.301: W/System.err(2385):     at com.impressol.appjiva.Home$MyTask.onPostExecute(Home.java:1)
    04-24 12:09:55.310: W/System.err(2385):     at android.os.AsyncTask.finish(AsyncTask.java:602)
    04-24 12:09:55.310: W/System.err(2385):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
    04-24 12:09:55.320: W/System.err(2385):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
    04-24 12:09:55.330: W/System.err(2385):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-24 12:09:55.330: W/System.err(2385):     at android.os.Looper.loop(Looper.java:137)
    04-24 12:09:55.330: W/System.err(2385):     at android.app.ActivityThread.main(ActivityThread.java:4340)
    04-24 12:09:55.330: W/System.err(2385):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-24 12:09:55.341: W/System.err(2385):     at java.lang.reflect.Method.invoke(Method.java:511)
    04-24 12:09:55.341: W/System.err(2385):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    04-24 12:09:55.341: W/System.err(2385):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    04-24 12:09:55.350: W/System.err(2385):     at dalvik.system.NativeStart.main(Native Method)
9
  • 1
    stacktrace from logcat please? update question with json response. Commented Apr 24, 2013 at 6:38
  • 1
    your json is not vaild, first check json. Commented Apr 24, 2013 at 6:39
  • @SankarV : Thanks for your quick response.I have edited my question with logcat details.Please see it. Commented Apr 24, 2013 at 6:43
  • @rajeshwaran : Dude this JSON is working fine in iPhone. Commented Apr 24, 2013 at 6:43
  • JSONObject is covered by {} and JSONArray covered by []. Commented Apr 24, 2013 at 6:43

3 Answers 3

2

Remove var test = at the start and ; at the end from JSON Response.

Your JSON parsing Android code should be as follow

try {
                    JSONArray jsonArray = new JSONArray(result);


                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                          System.out.println(c.getString(APP_URL));
                          System.out.println(c.getString(APP_NAME));
                          System.out.println(c.getString(APP_IMAGE));
                          System.out.println(c.getString(IMAGE_TYPE));
                          System.out.println(c.getString(APP_TRACK));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
Sign up to request clarification or add additional context in comments.

Comments

2

Remove "var test" from beginning and ";" from end of result string. So that the JSON looks like following..

[
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a00e0-acab-11e2-afa2-a0369f06db33/0/306833",
        "text": "Amazon",
        "img": "http://img.ads.mocean.mobi/img/50e/cbb/ed021d2/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a00e0-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a00e2-acab-11e2-afa2-a0369f06db33/0/306864",
        "text": "Call of Duty",
        "img": "http://img.ads.mocean.mobi/img/50e/cbe/c0c7c88/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a00e2-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a27f0-acab-11e2-afa2-a0369f06db33/0/306868",
        "text": "GPS Drive",
        "img": "http://img.ads.mocean.mobi/img/50e/cc0/42a71e8/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a27f0-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a27f3-acab-11e2-afa2-a0369f06db33/0/306764",
        "text": "Twitter",
        "img": "http://img.ads.mocean.mobi/img/50e/cb5/942aaab/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a27f3-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a4f00-acab-11e2-afa2-a0369f06db33/0/306829",
        "text": "Temple run Brave",
        "img": "http://img.ads.mocean.mobi/img/50e/cba/c30b077/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a4f00-acab-11e2-afa2-a0369f06db33"
    }
]

Then try changing following lines:

                JSONObject mainJson = new JSONObject(result);
                //JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
                JSONArray jsonArray = mainJson.getJSONArray(APP_ARRAYNAME);

With

                JSONArray jsonArray = new JSONArray(result);

You can always use http://jsonlint.org/ to check whether the JSON is valid or not...

1 Comment

what APP_ARRAYNAME , I faced the same problem I have an array without name how to fix it ?
1

I think you need to remove "var test = " from string parsing...

1 Comment

Yeah I removed "var test = " from the first and ";" from the end and checked the Json response for validation and its valid.

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.