1

我有一个 AsynTask,它从 Web 服务中检索数据,并在 UI 上查看这些数据。所以,在我的 MainActivity 中,我有一个 textView。

这是我从网络服务收到的数据:

{"name":"ezio","country":"italy"}{"name":"fufu","country":"tutu"}{"name":"chikaka","country":"aceVentura"}

问题是,我不知道如何使用 ClientConnection 类中的“结果”值设置 textView。当我运行应用程序时,textView 是空的。

公共类 ClientConnection 扩展 AsyncTask {

public static final String URL = "http://192.168.0.15/test.php";

static JSONObject jObj = null;
public static String result = "";

@Override
protected String doInBackground(Void... voids) {

    // public JSONObject connect(){
    try{
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        HttpResponse response = httpClient.execute(httpGet);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.e("HTTPStatus error:","Status not okay");
        }

        InputStream in = response.getEntity().getContent();
        BufferedReader reader =  new BufferedReader(new InputStreamReader(in, "iso-8859-1"), 8);
        StringBuilder str = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            str.append(line + "\n");
        }
        in.close();
        result = str.toString();

        JSONObject jsonObject = convertToJson(result);

        // jsonObject.get()
        //result = jsonObject.getString("name");
        //JSONArray google = jsonObject.getJSONArray("");


    } catch (Exception e) {
        //Toast toast = Toast.makeText(null, e.getMessage(), Toast.LENGTH_LONG);
        Log.e("Error","don't know what exception though");
    }

    return result;
}

private JSONObject convertToJson(String test){

    JSONArray clients = new JSONArray();

    try{
        jObj = new JSONObject(test);

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

    return jObj;
}

public String getResult(){
        return result;
    }
    public JSONObject getjObj(){
        return jObj;
    }
}

这是主要活动

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView textView = (TextView) findViewById(R.id.textViewTest);
        ListView listView = (ListView) findViewById(R.id.listView);
        Button buttonConnect = (Button) findViewById(R.id.buttonConnect);

        final ClientJSONParsingActivity clientJSONParsingActivity = new ClientJSONParsingActivity();

        buttonConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new ClientConnection().execute();

                textView.setText(new ClientConnection().getResult());

            }
        });
    }
}

感谢您的帮助

4

4 回答 4

2

您可以在 AsyncTask 中的 onPostExecute 中显示结果。

于 2012-12-19T23:51:44.140 回答
1

您应该在异步任务中更新 textview。 onPostExecute()方法在 UI 线程上运行

 protected void onPostExecute(String result) {
      textView.setText(result);
    }
于 2012-12-19T23:51:41.433 回答
0

将文本视图作为参数传递给 asynctask 并将其设置在 onPostExecute 中。在我的手机上,所以没有代码,抱歉 ;-)

于 2012-12-19T23:50:40.843 回答
0

在您的 doinbackground 下添加此代码;

 protected void onPostExecute(Long result) {
(find your text view here from the context where textview it is)
          textView.setText(result);
      }
于 2012-12-19T23:51:07.550 回答