麻烦JSON例外 - 的Andr​​oid [英] Trouble with JSON exception - Android

查看:178
本文介绍了麻烦JSON例外 - 的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么我得到一个JSON例外,而我分析我的JSON对象。我得到(HTTP GET)的JASON从URL。这里是所有相关的code,让我知道如果你需要看到任何更多code

I am having trouble figuring out why i'm getting a JSON exception while I am parsing my JSON object. I am getting(Http GET) the JASON from a URL. Here is all the relevant code, let me know if you need to see any more of the code

该doInBackground异步方法:

The doInBackground Async method:

@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.w("Rakshak", "the jaon String is:"+jsonStr);// this prints the JASON in the log and it looks fine
                                                        // I am not pasting it in coz it is HUGE 

        if (jsonStr != null)
        {
            try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    Log.w("Rakshak", "in the try before the JASON");

                    // Getting JSON Array node
                    kingtide = jsonObj.getJSONArray("JASON");

                    // looping through All Kingtide events
                    for (int i = 0; i < kingtide.length(); i++) 
                    {
                        JSONObject k = kingtide.getJSONObject(i);

                        String date = "Date Range:"+k.getString(KEY_DATE);
                        String lat = k.getString(KEY_LAT);
                        String lng = k.getString(KEY_LNG);
                        String loc = "Location of the Kingtide:"+k.getString(KEY_LOC)+", "+k.getString(KEY_STATE);
                        String temp_Time = k.getString(KEY_TIME);
                        String[] time_parts = temp_Time.split("T");
                        String time = "Kingtide at:"+time_parts[1]+" "+getYear(time_parts[0]);

                        // tmp hashmap for single kingtide event
                        HashMap<String, String> kt = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        kt.put(KEY_DATE, date);
                        kt.put(KEY_LAT, lat);
                        kt.put(KEY_LNG, lng);
                        kt.put(KEY_LOC, loc);
                        kt.put(KEY_TIME, time);

                        Log.w("Rakshak", KEY_DATE+KEY_LAT+KEY_LNG+KEY_LOC+KEY_TIME);

                        // adding the kingtide to the kingtide hash map. this will be used to fill up the list view
                        kingTideList.add(kt);
                    }

                } catch (JSONException e) {
                    Log.e("Rakshak", "JSONException "+e.getMessage());  // this prints "JSONException Value [{"Latitude":-19.9078861,"Location":"Abbot....." and the rest of the JASON(all of it) 
                }
        }
        else 

            Log.w("Rakshak", "JASON string is null"); 

        return null;
    }

在服务处理程序类:

the Service handler class:

    public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        Log.e("Rakshak", "UnsupportedEncodingException "+e.getMessage());
    } catch (ClientProtocolException e) {
        Log.e("Rakshak", "ClientProtocolException "+e.getMessage());
    } catch (IOException e) {
        Log.e("Rakshak", "IOException "+e.getMessage());
    }


    Log.w("Rakshak", "In the service handeler: this is a test");


    return response;

}

}

堆栈跟踪的部分:

03-14 10:09:56.861: E/Rakshak(7037): JSONException Value [{"Latitude":-19.9078861,"Location":"Abbot Point","Longitude":148.08467259999998,"DateRange":"1–3 January 2014","HighTideOccurs":"2014-01-02T09:47:00","State":"QLD"},{"Latitude":-27.477819,"Location":"Brisbane 

对于JASON文件的URL是 http://witnesskingtides.azurewebsites.net/api/kingtides

请注意:我知道它看起来像一个XML文件,但它是JASON。刚刚经历了验证/浏览器运行它,看看你的自我,如果你想要的。

NOTE: I know it looks like a XML file but it is JASON . Just run it through a validator/viewer and see for your self if you want.

我在为什么我得到一个JASON例外,如何解决它的问题。

My question in why am I getting a JASON exception and how do I fix it.

推荐答案

在获取内容,我得到这个后面(的一部分):

When getting the contents, I get this back (part of it):

[
    {
        "Location": "Abbot Point",
        "State": "QLD",
        "HighTideOccurs": "2014-01-02T09:47:00",
        "DateRange": "1–3 January 2014",
        "Latitude": -19.9078861,
        "Longitude": 148.08467259999998
    },
    {
        "Location": "Brisbane  Bar",
        "State": "QLD",
        "HighTideOccurs": "2014-01-02T10:16:00",
        "DateRange": "1–3 January 2014",
        "Latitude": -27.477819,
        "Longitude": 153.01889119999998
    },
    ...
]

这意味着你的对象已经是一个数组。尝试在code可以改变:

This means that your object is already an array. Try to change this in your code:

                //JSONObject jsonObj = new JSONObject(jsonStr);

                Log.w("Rakshak", "in the try before the JASON");

                // Getting JSON Array node
                kingtide = new JSONArray(jsonStr);

由于返回jsonStr已经是一个数组(而不是与所谓的杰森的数组属性的对象)。

since the returned jsonStr is already an array (and not an object with an array-attribute called "JASON").

这篇关于麻烦JSON例外 - 的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆