1

JSON オブジェクトの解析中に JSON 例外が発生する理由がわかりません。URLからJASONを取得(Http GET)しています。関連するすべてのコードは次のとおりです。これ以上のコードが必要な場合はお知らせください。

doInBackground Async メソッド:

@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;
    }

サービス ハンドラ クラス:

    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 です。必要に応じて、バリデーター/ビューアーを介して実行し、自分で確認してください。

なぜJASON例外が発生するのか、どうすれば修正できるのかという質問です。

4

5 に答える 5

0

更新:以下の私の答えを無視してください...

それが JSON であるという事実の繰り返しの概念を爆発させないために、そうではありません。

コードが返す応答はプレーンな XML です。

でも、

要求しているリソース ( http://witnesskingtides.azurewebsites.net/api/kingtides ) は、XML 形式の応答と JSON 形式の応答の両方をサポートしています。おそらくAccept、コード内のリクエストに欠落しているヘッダー、またはに設定されているヘッダーapplication/xmlに関係している可能性がtext/xmlありますServiceHandler

コードがサーバーの応答を受け取ると、サーバーはAcceptヘッダーを見つけられず、XML 形式を返します。

あなたが言及したJSONバリデーターサイトが同じURLを要求するとAccept、サーバーにJSON形式で応答を返すように指示するヘッダーが追加される可能性があります。

ServiceHandlerクラスがどのように機能するかはわかりませんが、GETリクエストを作成するときは、HTTP ヘッダーに名前Acceptと値を追加してapplication/jsonから、リクエストを発行する必要があります。XML の代わりに JSON が返されるようになりました。

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

于 2014-03-14T13:39:20.513 に答える