android - 使用 AsyncTask 从解析的 JSON 对象中设置文本

标签 android json android-asynctask httpclient

我正在制作一个 Android 程序,用于从互联网网页的源代码中解析 JSON 文本。它在 android 2.2 中工作,但我现在需要它在 android 3.0 上,它需要在 AsyncTask 上。我有关于 AsyncTask 的背景,但我很困惑该把这个放在哪里。预先感谢大家:)

这是我在 MainActivity 类中的方法:

private void jsonStuffs() {
    //JSON PARSER & HOME PAGE TEXTVIEWS

        client = new DefaultHttpClient();

        GetMethodEx test = new GetMethodEx();
        String returned;
        try {
            returned = test.getInternetData();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try{
            String jsonStr = test.getInternetData(); //go to GetMethodEx
                JSONObject obj = new JSONObject(jsonStr);

//////////////////////find temperature in the JSON in the webpage

                String temperature = obj.getString("temperature");
                TextView tvTemp = (TextView)findViewById(R.id.textView);
                tvTemp.setText(temperature);

        }
        //catch (JSONException e) {
             // e.printStackTrace();
            //} 
        catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

GetMethodEx 类是这样的(这将找到网页的链接,然后将其源代码转换为文本格式):

public class GetMethodEx extends Activity {
    public String getInternetData() throws Exception{

        BufferedReader in = null;
        String data = null;
        //

        try{
            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://nhjkv.comuf.com/json_only.php");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) !=null){
                sb.append(l + nl);
            }
            in.close();
            data = sb.toString();
            return data;
        }finally {
            if (in !=null){
                try{
                    in.close();
                    return data;
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

最佳答案

你可以这样做(此代码仅用于说明,根据需要进行更改)

class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
     protected void onPreExecute() {
        // You can set your activity to show busy indicator
        //setProgressBarIndeterminateVisibility(true);
     }

    protected JSONObject doInBackground(String... args) {
        return jsonStuffs();
    }

    protected void onPostExecute(final JSONObject jsonObj) {
        String temperature = jsonObj.getString("temperature");
        TextView tvTemp = (TextView)findViewById(R.id.textView);
        tvTemp.setText(temperature);
        // Stop busy indicator
        //setProgressBarIndeterminateVisibility(false);
    }

要调用此任务,请使用new MyAsyncTask().execute();(如果需要,您可以传递String参数来执行) 您可以更改 jsonStuffs() 以返回 JSONObject

例如

private JSONObject jsonStuffs() {

     // ...

     String jsonStr = test.getInternetData(); //go to GetMethodEx
     return new JSONObject(jsonStr);

     // ...
 }

关于android - 使用 AsyncTask 从解析的 JSON 对象中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808111/

相关文章:

android - 循环中的异步任务?

Android - 使用 AsyncTasks 的正确方法?

android - 如何从 AsyncTask 更新 ListView?

android - 测试最近启动的 Activity 是否真的已经开始

android - Android中如何获取应用程序的启动时间

c# - 我应该为来自 TextBox 控件的序列化数据指定哪种编码

json - 将 JSON 中的字符串(字符串数组)解析为 Swift 中的字符串数组

c# - 老大说 "We need an IPhone app",类似iPhone上的Firefox、JQuery、模板、数据链接的框架有哪些?

java - 无法从 Samsung Galaxy S (2.2.1) 上的前置摄像头拍照

c# - 奇怪的反射问题 - 无法得到它