0

我正在尝试使用 Asynctask 方法从 Web 服务器获取数据。

我有以下代码。

package com.example.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class FetchData extends Activity {
private TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fetch_data);
    textView = (TextView) findViewById(R.id.TextView1);


    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
}

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
              HttpResponse execute = client.execute(httpGet);
              InputStream content = execute.getEntity().getContent();

              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }

            } catch (Exception e) {
              e.printStackTrace();
            }
          }
          return response;
        }

        @Override
        protected void onPostExecute(String result) {
          textView.setText(result);
        }
      }

      public void readWebpage(View view) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] {message});

}



}

我的问题是这个 FetchData Activity 又必须从 MainActivity 接收数据。我已经使用意图这样做了。现在,我在这里使用的意图具有来自 MainActivity 的数据。此消息包含 Web 服务器的 URL。我只需要使用消息从网页获取结果。我想将此消息传递给 task.execute 方法,以便它可以将网页返回给我。我该怎么做。?

4

2 回答 2

1

readWebpage除了方法调用之外,您似乎拥有所有您需要的东西。

  1. 更改readWebpage传递消息的参数:

    public void readWebpage(String message) {
      DownloadWebPageTask task = new DownloadWebPageTask();
      task.execute(new String[] {message});
    }
    
  2. readWebpage在最后一行添加调用onCreate

    ...
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    readWebpage( message );
    
于 2012-11-30T08:01:53.210 回答
0
mWebView.setWebViewClient(new WebViewClient() {
        private int       webViewPreviousState;
        private final int PAGE_STARTED    = 0x1;
        private final int PAGE_REDIRECTED = 0x2;

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
            webViewPreviousState = PAGE_REDIRECTED;
            mWebView.loadUrl(urlNewString);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            webViewPreviousState = PAGE_STARTED;
            if (dialog == null || !dialog.isShowing())
                dialog = ProgressDialog.show(WebViewActivity.this, "", getString(R.string.loadingMessege), true, true,
                        new OnCancelListener() {

                            @Override
                            public void onCancel(DialogInterface dialog) {
                                // do something
                            }
                        });
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            if (webViewPreviousState == PAGE_STARTED) {
                dialog.dismiss();
                dialog = null;
            }

        }
 });
于 2012-11-30T08:12:17.257 回答