我的代码不会返回任何字符串

时间:2013-06-12 06:42:53

标签: android jsoup

我正在尝试使用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);


 }
}

3 个答案:

答案 0 :(得分:1)

由于AsyncTask的doInBackground()具有 void返回类型,只需将其更改为字符串,然后将myString代替null返回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

答案 1 :(得分: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>

快照模拟器

enter image description here

html代码中的标题1

enter image description here

或者

编辑:

在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
    }

}
}

答案 2 :(得分:-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);


 }
}