-1

こんにちは、mysql データベースを取得するためのこのコードがありますが、情報を解析するときにエラーが発生します。@Override protected Void doInBackground(String... params) {

    String result = "";
    InputStream is = null;

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://cs1.ucc.ie/~am32/getDB.php");
            //httppost.setEntity(new UrlEncodedFromEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }
    catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());

    }
    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");

            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line);
            }
            is.close();
            result=sb.toString();


            Log.i("json string", result);
    }
    catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

    //parse json data
    try{
            List<String> myList= new ArrayList<String>(Arrays.asList(result.split(" ")));
            JSONArray jArray = new JSONArray(myList);

            System.out.println("Length"+ jArray.length());
            Log.d("DB","Length"+jArray.length());

            for(int i=0; i<jArray.length(); i++){

                    JSONObject json_data = null;

                    json_data = jArray.getJSONObject(i);
                    int id = i +1;
                    //String title = json_data.getString("Title"); 
                    double latitude = json_data.getDouble("Lat"); 
                    double longitude = json_data.getDouble("Lon"); 

                    //Adds proximity to POI's
                    ProxAlert inst = new ProxAlert();
                    inst.addProximityAlert(latitude,longitude, id);
                    //
                    inst.saveCoordinatesInPreferences((float)latitude, (float)longitude);

                    //prints to logCat
                    System.out.println(id+"&"+latitude+"&"+longitude);


            }
    }
    catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
            Log.e("log_tag","Failed data as:\n"+result);
    }
        return null;
}

そしてlogCatは読み取ります

09-04 19:13:13.997: E/log_tag(28100): Error parsing data org.json.JSONException: Value  at 0 of type java.lang.String cannot be converted to JSONObject
4

1 に答える 1

0

コードでは、http リクエスト ( http://cs1.ucc.ie/~am32/getDB.php ) を実行し、次のようなコンテンツを取得します。

51.899429 -8472801 51.898525 -8.472176 51.899522 -8.47293 51.899495 -8.472844 51.899456 -8.472823 51.899628 -8.473016 51.899442 -8.47278 51.89931 -8.472587 51.808514 -8.472248 51.899174 -8.472649 51.899469 -8.472823 51.8932045 -8.500971 51.893623 -8.501472

これは有効な Json ではありません。

Json Validation をチェックするには、 JsonLint を使用します

[編集] 理解を深めるために、まず Json ドキュメントをお読みください。ここでandroid-json-parsing-tutorialは、例を使用して Json の解析を説明する優れたチュートリアルです。

于 2013-09-04T18:10:57.997 に答える