0

私はjson解析を使用してアプリに取り組んでいます...この解析では、指定されたURLのjsonによって行われます。

target = "Google APIs (Google Inc.) - API level 10" を持つエミュレーターでプロジェクトを実行すると、正しく実行され、ターゲット URL から必要な結果が表示されます。

しかし、target = "Google APIs (Google Inc.) - API level 16" を持つエミュレーターでプロジェクトを実行すると、エラーが表示され、指定された URL データを解析して強制終了することはありません。

すべての API レベルで動作するアプリを作りたいです。

助けてください...

ここに私のコードがあります:

json パーサー クラス:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONArray jObj = null;
    static String json = "";
    static String req = "POST";


    // constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromUrl(String url, String method) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse httpResponse = null;
            if(method == req) {
              HttpPost httpC = new HttpPost(url);
              httpResponse = httpClient.execute(httpC);
            }else {
              HttpGet httpC = new HttpGet(url);
              httpResponse = httpClient.execute(httpC);
            }
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        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();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

json パーサー クラスとフェッチ データを使用する別のクラス:

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class showData extends ListActivity{

    public static String url = "http://something/something/";

    public static final String TAG_A = "a";
    public static final String TAG_B = "b";
    public static final String TAG_C = "c";
    public static final String TAG_D = "d";
    public static final String TAG_E = "e";
    public static final String TAG_F = "f";
    public static final String GET = "get";

    JSONArray Data1 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
        EditText editext_text = (EditText) findViewById(R.id.et);
        String urlnew = url + editext_text.getText().toString();

        Log.d("url", urlnew);

        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        area1 = jParser.getJSONFromUrl(urlnew, GET);


            Log.d("Json String", area1.toString());

            try {

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

                JSONObject c = area1.getJSONObject(i);

                // Storing each json item in variable
                String a = c.getString(TAG_A);
                String b = c.getString(TAG_B);
                String c = c.getString(TAG_C);
                String d = c.getString(TAG_D);
                String e = c.getString(TAG_E);

HashMap<String,String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_A, a);
                map.put(TAG_B, b);
                map.put(TAG_C, c);
                map.put(TAG_D, d);
                map.put(TAG_E, e);

                // adding HashList to ArrayList
                contactList.add(map);
            }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            ListAdapter adapter = new SimpleAdapter(this, contactList,
                    R.layout.list_item_area,
                    new String[] { TAG_B, TAG_A, TAG_C, TAG_D, TAG_E }, new int[] {
                            R.id.b, R.id.a, R.id.c, R.id.d, R.id.e });

            setListAdapter(adapter);            

        }


    }
4

2 に答える 2