-1

我正在尝试使用异步任务从使用 json 数组的 URL 加载数据。

我想在每次调用 AsyncTask 时传递一个 URL。

在此示例中,我想在 info 类中调用 AsyncTask 我还想在文本视图中显示数组中的值。

我将不胜感激任何帮助。谢谢。

信息.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
    android:id="@+id/infoTxtLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TextView
        android:id="@+id/InfoTxt"
        android:layout_width="match_parent"
        android:layout_gravity="top|left"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/InfoTxt"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

信息.java

public class Info extends Activity {

TextView resultView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);
        resultView = (TextView) findViewById(R.id.dbtext);
        new DB().execute();// calling async task
    } catch (Exception e) {
        Log.e("Log_tag", "Error in opening Info page" + e.toString());
        resultView.setText("couldnt load info page");
    }
}
}

浏览器的结果

[{"client_id":"1","client_name":"client1"},{"client_id":"2","client_name":"client2"}]

db.java

public class DB extends AsyncTask<URL, Integer, String> {
TextView resultView;
String result = "";
String s = "";
InputStream isr = null;

@Override
protected String doInBackground(URL... params) {

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://127.0.0.1/site/client.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    } catch (Exception e) {
        Log.e("Log_tag", "Error in Http connection" + e.toString());
        resultView.setText("couldnt connect to the database");
    }

    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                isr, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        isr.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result" + e.toString());
    }

    // parse the json data
    try {

        JSONArray jArray = new JSONArray(result);

        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json = jArray.getJSONObject(i);
            s = s + "ID :" + json.getInt("client_id") + "\n" + "NAME :"
                    + json.getString("client_name");

        }
        //resultView.setText(s);

    } catch (Exception e) {
        Log.e("log_tag", "Error Parsing Data" + e.toString());
    }
    return s;

}


protected void onProgressUpdate(Integer... progress) {
    // setProgressPercent(progress[0]);
}

protected void onPostExecute(Long result) {
    // showDialog("Downloaded " + result + " bytes");
    if (s!= null){
        resultView.setText(s);
    }
}
}
4

1 回答 1

1

在这里 :

protected void onPostExecute(Long result) {  //<<<<<< method argument is different 

    if (s!= null){
        resultView.setText(s);
    }
}

目前您没有onPostExecute从 AsyncTask 覆盖方法,因为doInBackground方法返回类型是但是您正在String传递参数。因此,只需将您从 AsyncTask 覆盖的所有方法放在前面:LongonPostExecute@Override

@Override
protected void onPostExecute(String result) {  //<<<< change to String from Long

    if (s!= null){
        resultView.setText(s);
    }
}
于 2013-05-23T11:31:00.537 回答