-2

我正在尝试使用 jsoup 从网站返回字符串,但我的代码在 Jsoup.connect 之后中断并且它不返回任何字符串

我的代码是:

    class fetcher extends AsyncTask<Void,Void,Void>{
 String myString = null;
 @Override
    protected Void doInBackground(Void... arg0) { 
        Document doc = null;
        try {

            doc = Jsoup.connect("http://www.ismmusalla.org/").get();

            Elements divs = doc.select("div#title1");


                for (Element div : divs) {

                    myString=myString+" " +div.text();


                      }
        }
        catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();

        }
        return null;
}
 protected void onPostExecute(String result)
 {
     textview.setText(myString);


 }
}
4

3 回答 3

1

因为您的 AsyncTask 的返回类型doInBackground()void,只需将其更改为String并放置myString而不是null作为 return data

像,

protected String doInBackground(Void... arg0) {

 return myString;
}

AsyncTask 的声明也不合适,extends AsyncTask<Void,Void,Void>应该是extends AsyncTask<Void,Void,String>.

看看http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-06-12T06:49:05.360 回答
1
public class MainActivity extends Activity {
    String myString="";
    TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tv = (TextView) findViewById(R.id.textView1);
  new TheTask().execute();

}


class TheTask extends AsyncTask <Void,Void,Void>
{

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        Document doc = null;
        try {

            doc = Jsoup.connect("http://www.ismmusalla.org/").get();

            Elements divs = doc.select("div#title1");
                for (Element div : divs) {
                    myString=myString+" " +div.text();
                      }
        }
        catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        tv.setText(myString);
    }

}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
     >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

模拟器截图

在此处输入图像描述

您在 html 代码中的标题 1

在此处输入图像描述

或者

编辑:

在 doInbackground 中返回字符串并在 onPostExecute 中更新 textview

public class MainActivity extends Activity {    
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tv = (TextView) findViewById(R.id.textView1);
  new TheTask().execute();

}

class TheTask extends AsyncTask <Void,Void,String> // change Void to String
{
    String myString="";
    @Override
    protected String doInBackground(Void... arg0) { // return type is string
        // TODO Auto-generated method stub
        Document doc = null;
        try {

            doc = Jsoup.connect("http://www.ismmusalla.org/").get();

            Elements divs = doc.select("div#title1");
                for (Element div : divs) {

                    myString=myString+" " +div.text();
                      }
        }
        catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();

        }
        return myString; //return string result
    }

    @Override
    protected void onPostExecute(String result) { // recieve  string result
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        tv.setText(result); // update textview with string result
    }

}
}
于 2013-06-12T07:00:50.950 回答
-1

使用以下代码:

class fetcher extends AsyncTask<Void,Void,String>{
 String myString = "";
 @Override
    protected String doInBackground(Void... arg0) { 
        Document doc = null;
        try {

            doc = Jsoup.connect("http://www.ismmusalla.org/").get();

            Elements divs = doc.select("div#title1");


                for (Element div : divs) {

                    myString=myString+" " +div.text();


                      }
        }
        catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();

        }
        return myString;
}
 protected void onPostExecute(String result)
 {
     textview.setText(result);


 }
}
于 2013-06-12T06:46:38.453 回答