1

SQLiteDBをhttpクエリのエントリで埋める関数があります。

try {
        stringEntity = new StringEntity(SQL);
        httpPost.setEntity(stringEntity);
        httpResponse = httpClient.execute(httpPost);
        httpEntity = httpResponse.getEntity();
        bos = new ByteArrayOutputStream();
        httpEntity.writeTo(bos);

        data = bos.toString();

        reader = new BufferedReader(
                  new StringReader(data));

        try {
            //SAVE DATA IN MY DB || WORKS
        } catch(IOException e) {
          e.printStackTrace();
        }

    } catch (IOException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }

私がやろうとしていることは、手順が始まる前に(私のコードの最初の「try {..」の前に)私のアクティビティにテキストビューのテキストを設定することです。しかし、私の活動は忙しくてデータを取得できないため、テキストは変更されません(他に説明はありません。)

助言がありますか?

ありがとう、prexx

UPDATE''AsyncTask からデータを取得する''

 txtAction.setText("Loading...");

    AsyncTask<String, String, String> test = new cAsyncTask();

    try {
        data = test.execute(URL).get();

        reader = new BufferedReader(
                  new StringReader(data));

        while ((line = reader.readLine()) != null) {
            //SAVE DATA IN DB || WORKS
            }
        }

    } catch(IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

私の非同期タスク:

class cAsyncTask extends AsyncTask<String, String, String> {

protected String doInBackground(String... urls) {
    int count = urls.length;
    String data = "";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost;
    StringEntity stringEntity;
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    ByteArrayOutputStream bos;
    String line;
    BufferedReader reader;
    for (int i = 0; i < count; i++) {
        httpPost = new HttpPost(urls[i].toString());
        try {
            stringEntity = new StringEntity(SQL);
            httpPost.setEntity(stringEntity);
            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            bos = new ByteArrayOutputStream();
            httpEntity.writeTo(bos);

            data = bos.toString();

        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
    }
    return data;
}

protected void onProgressUpdate(String... progress) {

}

protected void onPostExecute(String result) {
    String test = result;
}
4

3 に答える 3

4

Put the busy part of your code into a separated Thread.

Look at the AsyncTask utility

Call AsyncTask.execute() just after textview.setText("foo") and u will be fine :)

Regards

UPDATE with code sample:

 txtAction.setText("Loading...");
 AsyncTask<String, String, String> test = new cAsyncTask();
 test.execute("http://...");

class cAsyncTask extends AsyncTask<String, String, String> {

protected String doInBackground(String... urls) {
    int count = urls.length;
    String data = "";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost;
    StringEntity stringEntity;
    HttpResponse httpResponse;
    HttpEntity httpEntity;
    ByteArrayOutputStream bos;
    String line;
    BufferedReader reader;
    for (int i = 0; i < count; i++) {
        httpPost = new HttpPost(urls[i].toString());
        try {
            stringEntity = new StringEntity(SQL);
            httpPost.setEntity(stringEntity);
            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            bos = new ByteArrayOutputStream();
            httpEntity.writeTo(bos);

            data = bos.toString();

        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
    }
     reader = new BufferedReader(
                  new StringReader(data));
       String line = "";
        while ((line = reader.readLine()) != null) {
            //SAVE DATA IN DB || WORKS
     }
    return data;
}


protected void onPostExecute(String result) {
    String test = result;
   textView.setText("Done!");
}

}

The key is to put all busy code into the doInBackGround method which will runs in a separate thread. All UI modification must be in the same UI thread and this could be done into the onPostExecute method which will be executed in the same UI thread

于 2011-06-13T15:12:40.993 に答える
0

「忙しすぎる」とは関係ありませんが、メソッドが戻ったときにのみテキストが設定されるという事実とは関係ありません。そしてあなたのネットワーキングではこれは遅れるでしょう。

ところで。UIスレッドのHoneycombネットワークでは、例外がスローされ、アプリが強制終了されます。

于 2011-06-13T15:37:38.413 に答える
0

TextViewでinvalidate()を呼び出そうとすることができます。ただし、非同期タスクを使用することは、大量のデータをロードする方法のベストプラクティスです。これにより、ユーザーの操作UIコントロールが中断されることはありません。

于 2011-06-13T15:19:11.767 に答える