0

我有这样JSONparser的课:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            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, "UTF-8"), 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 JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

和这样的活动:

public class BankExchangersListActivity extends ExpandableListActivity  {

    private static String url;

    // JSON Node names
    private static final String TAG_Exchangers = "bank_exchangers";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_address = "address";
    private static final String TAG_location_name = "location_name";
    private static final String TAG_latitude = "latitude";
    private static final String TAG_longitude = "longitude";
    private static final String TAG_exchanger_type_name = "exchanger_type_name";
    private static final String TAG_exchanger_curr_value = "value";
    private static final String TAG_currency_list_name = "currency_list_name";
    private static final String TAG_direction_of_exchange_name = "direction_of_exchange_name";

    JSONArray banks = null;
    JSONArray exc_currencies = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String bank;
        bank = this.getIntent().getStringExtra("Bank_id");
        url = "****/**_**_***_list/"+bank+".json";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_exchangers_list);

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();


        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        ArrayList result = new ArrayList();
        try {
            // Getting Array of Contacts
            banks = json.getJSONArray(TAG_Exchangers);

            // looping through All Contacts
            for(int i = 0; i < banks.length(); i++){
                JSONObject c = banks.getJSONObject(i);
                exc_currencies = c.getJSONArray("currency_values");
                HashMap<String, String> map2 = new HashMap<String, String>();
                ArrayList secList = new ArrayList();
                for(int k = 0; k < exc_currencies.length(); k++){
                    JSONObject m = exc_currencies.getJSONObject(k);
                    String currency = m.getString(TAG_exchanger_curr_value); 
                    String currency_list_name = m.getString(TAG_currency_list_name); 
                    String direction_of_exchange_name = m.getString(TAG_direction_of_exchange_name); 
                    Log.e("wazzzup", currency); //here is trouble: how to do new array with linking to parent?
                    HashMap child = new HashMap();
                    child.put(TAG_exchanger_curr_value, currency );
                    child.put(TAG_currency_list_name, currency_list_name );
                    child.put(TAG_direction_of_exchange_name, direction_of_exchange_name );
                    secList.add(child);
                }
                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                Log.e("name", name); //here is trouble: how to do new array with linking to parent?
                String address = c.getString(TAG_address);
                String location_name = c.getString(TAG_location_name);
                String latitude = c.getString(TAG_latitude);
                String longitude = c.getString(TAG_longitude);
                String exchanger_type_name = c.getString(TAG_exchanger_type_name);

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


                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_address, address);
                map.put(TAG_location_name, location_name);
                map.put(TAG_latitude, latitude);
                map.put(TAG_longitude, longitude);
                map.put(TAG_exchanger_type_name, exchanger_type_name);


                // adding HashList to ArrayList
                contactList.add(map);
                result.add(secList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        /*ListAdapter adapter = new SimpleAdapter(this, contactList,
                R.layout.bank_exchanger_list_element,
                new String[] { TAG_NAME, TAG_location_name, TAG_address, TAG_exchanger_type_name, TAG_latitude, TAG_longitude }, new int[] {
                        R.id.bank_e_n, R.id.nas_punkt_e_n , R.id.adress_obm_e_n , R.id.tip_obm_e_n , R.id.shirota_e_n , R.id.dolgota_e_n });

        setListAdapter(adapter);*/

                SimpleExpandableListAdapter expListAdapter =
                        new SimpleExpandableListAdapter(
                                this,
                                contactList,              
                                R.layout.bank_exchanger_list_element,             
                                new String[] { TAG_NAME, TAG_location_name, TAG_address, TAG_exchanger_type_name, TAG_latitude, TAG_longitude },
                                new int[] {
                                        R.id.bank_e_n, R.id.nas_punkt_e_n , R.id.adress_obm_e_n , R.id.tip_obm_e_n , R.id.shirota_e_n , R.id.dolgota_e_n },   
                                result,              //something goes here
                                R.layout.exchanger_currencies,             
                                new String[] {TAG_exchanger_curr_value, TAG_currency_list_name, TAG_direction_of_exchange_name},      
                                new int[] { R.id.currencyvalue_e_n, R.id.currency_list_name_e_n, R.id.direction_of_exchange_e_n}    
                            );
                        setListAdapter( expListAdapter ); 
                        ExpandableListView elv = (ExpandableListView) getExpandableListView();
                        for(int i=0; i < expListAdapter.getGroupCount(); i++)
                            elv.expandGroup(i);

    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bank_exchangers_list, menu);
        return true;
    }

}

在 android 2.3.3 上它可以正常工作。但是在 4.xi 上看到错误,搜索后我明白我需要使用AsyncTask. 这是真的吗?

但是我如何在这里使用它?如果有任何有用的例子,请告诉我。

只是如何让它在 Android 4.x 上运行?

4

1 回答 1

2

您应该已经在 Android 2.x 下使用 AsyncTask。它极大地提高了响应能力。这是我的 MEGA API 库中的一个片段(目前正在开发中):

    private class AsyncRequestConnection extends AsyncTask<Void, Void, String> {
    private final Request request;

    public AsyncRequestConnection(Request request) {
        this.request = request;
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            HttpPost p = createRequestHttpMessage(request);
            String resp = new String(stripResponse(getRequestClient().execute(p)));
            Log.v(TAG, resp);
            return resp;
        } catch (Exception e) {
            Log.e(TAG, "Cannot complete API request", e);
            cancel(false);
            return null;
        }
    }

    @Override
    protected void onCancelled() {
        request.backoff();
        if (request.hasReachedMaxBackoff()) {
            request.cancel(R.string.error_internal);
        } else {
            requestQueue.enqueue(request);              
        }
        requestConnection = null;
        nextRequest();
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
            request.attachResponse(result);
            request.handleResponse();               
            requestConnection = null;
            nextRequest();
        } else {
            request.cancel(R.string.error_internal);
        }
    }
}

doInBackgroud()是在不同线程上运行的唯一方法。在这里,您所有昂贵的操作都应该发生。 getRequestClient()返回一个HttpClientAndroidHttpClient.newInstance(AGENT_NAME)或为连续的多个请求重用的对象)。由于您在这里运行多个线程,请确保doInBackground()不访问任何全局数据结构。在我的示例中,getRequestClient()肯定只能从该位置调用 get,并且任何时候都只有一个这样的 AsyncTask。否则你需要某种互斥锁。此外,该Request对象肯定会被此类排他性使用。事件处理(回调方法)也在Request对象中实现,但对于更简单的任务,你可以简单地做你想做的一切,onCancel()并且onPostExecute(). 如果您只想下载 JSON 对象并对其进行解析,您可能甚至不需要构造函数和私有成员变量。只需将第一个Voidby替换String为将 URL 字符串传递给JSONObjectdoInBackground()并替换String为。

于 2013-05-01T19:54:42.653 回答