当调用扩展AsyncTask的类时,参数似乎不接收发送的String

时间:2014-12-24 04:50:34

标签: java android android-asynctask

我试着尽可能地解释我的问题,如果它没有意义那就很抱歉。 我遇到的问题是当我在onCreate方法中调用asyncTask类时,String似乎没有传递给doInBackground

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Timer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class MainActivity extends Activity {

String question;
TextView price;
Document document;
TextView article;
private Element overview;
EditText respText;
TextView text;
TextView ans;
String siteUrl;

private class ParseURL extends AsyncTask<String, Void, String> {

     @Override
     protected String doInBackground(String... strings) {
         StringBuffer buffer = new StringBuffer();
         try {
             Log.d("JSwa", "Connecting to ["+strings[0]+"]");
             Document doc  = Jsoup.connect(strings[0]).get();
             Log.d("JSwa", "Connected to ["+strings[0]+"]");
              //Get document (HTML page) title
             String title = doc.title();
             Log.d("JSwA", "Title ["+title+"]");
             buffer.append("Title: " + title + "\r\n");

             // Get meta info
             Elements metaElems = doc.select("meta");
             buffer.append("META DATA\r\n");
             for (Element metaElem : metaElems) {
                 String name = metaElem.attr("name");
                 String content = metaElem.attr("content");
                 buffer.append("name ["+name+"] - content ["+content+"] \r\n");
             }

             Elements topicList = doc.select("h2.topic");
             buffer.append("Topic list\r\n");
             for (Element topic : topicList) {
                 String data = topic.text();

                 buffer.append("Data ["+data+"] \r\n");
             }

         }
         catch(Throwable t) {
             t.printStackTrace();
         }

         return buffer.toString();
     }



     @Override
     protected void onPostExecute(String s) {
         super.onPostExecute(s);
         ans.setText(s);
     }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText edtUrl = (EditText) findViewById(R.id.edtURL);
    Button btnGo = (Button) findViewById(R.id.btnGo);
    //respText = (EditText) findViewById(R.id.edtResp);
    ans = (TextView) findViewById(R.id.textView2);
    siteUrl = edtUrl.getText().toString();

    btnGo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // String siteUrl = edtUrl.getText().toString();
            ( new ParseURL() ).execute(new String[]{siteUrl});
        }
    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

点击按钮而不是EditText方法内的onCreate获取文字:

btnGo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       siteUrl = edtUrl.getText().toString(); 
       new ParseURL().execute(siteUrl);
    }
});