3

JSON 配列をダウンロードすると、文字列の 1/4 が切り捨てられます。これはかなり巨大ですが、文字列全体を取得する必要があります。

LogCat にスローされるエラーはありません。これは私が使用している方法です。何度か経験しましたが、切断される理由がわかりません。しかし、私はこれにかなり慣れていません。

public static JSONArray getJSONfromURL(String url){

    //initialize
    InputStream is = null;   
    String result = "";   
    JSONArray jArray = null;

    //http post
    try {

        HttpClient httpclient = new DefaultHttpClient();   
        HttpPost httppost = new HttpPost(url);    
        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();   
        String line = null;

        while ((line = reader.readLine()) != null) {   
            sb.append(line + "\n");
        }

        is.close();
        result=sb.toString();

    } catch (Exception e) {    
        Log.e("log_tag", "Error converting result "+e.toString());
    }
    //try parse the string to a JSON object

    try {
        Log.d("log_tag", "jresult: " + result + "finish");
        jArray = new JSONArray(result);

        //Log.e("log_tag", "result: " + jArray.toString());

    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;
}

これが解けたら固まると思います。これを将来のプロジェクトでも使用するクラスにするので、再構築し続ける必要はありません!

編集:マーカーをマップに追加するループの場合:

try{
        for(int i=0;i<arrayResultFromURL.length();i++){
            JSONObject json_data = arrayResultFromURL.getJSONObject(i);

            // assign attributes from the JSON string to local variables
            String id =json_data.getString("id");
            String title =json_data.getString("title");
            String strap =json_data.getString("strap");
            Double lat = (double) json_data.getDouble("lat");
            Double lon = (double) json_data.getDouble("long");

            theMap.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lon))
            .title(title)
            .snippet(strap));
        }
    }catch (Exception e){
        Log.e("log_tag", "error in array: " + e.toString());
    }
4

2 に答える 2