1

Webサービスからデータを取得し、このデータを使用してUIに表示するAsynTaskがあります。したがって、MainActivityにはtextViewがあります。

これは私がWebサービスから受け取ったデータです:

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

問題は、ClientConnectionクラスの「result」の値で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

asynctask で 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 に答える