2

我有个问题。我使用 Jsoup 来解析网站。为此,我使用 AsyncTask。但很多时候数据没有得到。当我尝试使用此代码时:

public class ShowBar extends AsyncTask<String, Void, String>
{
    ProgressDialog mProgressDialog;
    public String info = "empty";
    private  boolean today = true;
    private Context  context_dialog;
    private OnCompleteListener mOnCompleteListener;



    public ShowBar(Context context, boolean day)
    {
        context_dialog = context;

        today = day;

    }

    public void setOnCompleteListener(OnCompleteListener listener) {
        mOnCompleteListener = listener;
    }




    @Override
    protected void onPostExecute(String result) {


            mOnCompleteListener.OnComplete(info,true);

        super.onPostExecute(result);
        mProgressDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog =
                ProgressDialog.show
                        (context_dialog
                                , "Loading"
                                , "Please wait", true);

        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        //publishProgress(new Void[] {});

         core();

        return info;

    }

     void core()
     {
         Elements links;

         final String URL = "http://dissp.com/translation/sport/footbol";
         final String YesterdayURL = "http://dissp.com/translation/sport/footbol/start";



         if (today)
         {

             Document doc = null;
             try {
                 doc = Jsoup.connect(URL).get();
                 links = doc.select("table[class=translation]");
                 info =  links.outerHtml();



             }catch (Exception e){
                 Log.e("errors","try-");
                 core();
             }

         }
     }

在日志中写道:

03-12 22:45:50.809: ERROR/errors(24683): try-
03-12 22:45:52.519: ERROR/errors(24683): try-
03-12 22:45:52.709: ERROR/errors(24683): try-
03-12 22:45:53.879: ERROR/errors(24683): try-
03-12 22:45:55.579: ERROR/errors(24683): try-
03-12 22:45:55.779: ERROR/errors(24683): try-
03-12 22:45:56.949: ERROR/errors(24683): try-
03-12 22:45:58.659: ERROR/errors(24683): try-
03-12 22:45:58.859: ERROR/errors(24683): try-
03-12 22:46:00.099: ERROR/errors(24683): try-
03-12 22:46:01.719: ERROR/errors(24683): try-
03-12 22:46:01.929: ERROR/errors(24683): try-
03-12 22:46:03.179: ERROR/errors(24683): try-
03-12 22:46:04.799: ERROR/errors(24683): try-
03-12 22:46:04.999: ERROR/errors(24683): try-

等等....

堆栈跟踪:

03-13 12:22:32.239: INFO/InputDispatcher(176): Delivering key to current input target
03-13 12:22:32.239: INFO/PowerManagerService(176): Ulight 3->7|0
03-13 12:22:32.319: INFO/InputDispatcher(176): Delivering key to current input target
03-13 12:22:32.509: WARN/System.err(25804): java.net.SocketTimeoutException
03-13 12:22:32.519: WARN/System.err(25804): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:461)
03-13 12:22:32.519: WARN/System.err(25804): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
03-13 12:22:32.519: WARN/System.err(25804): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:65)
03-13 12:22:32.519: WARN/System.err(25804): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:140)
03-13 12:22:32.529: WARN/System.err(25804): at java.io.BufferedInputStream.read(BufferedInputStream.java:225)
03-13 12:22:32.529: WARN/System.err(25804): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readLine(HttpURLConnectionImpl.java:660)
03-13 12:22:32.529: WARN/System.err(25804): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.readResponseHeaders(HttpURLConnectionImpl.java:690)
03-13 12:22:32.529: WARN/System.err(25804): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1040)
03-13 12:22:32.529: WARN/System.err(25804): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)
03-13 12:22:32.529: WARN/System.err(25804): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:412)
03-13 12:22:32.529: WARN/System.err(25804): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:393)
03-13 12:22:32.529: WARN/System.err(25804): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:159)
03-13 12:22:32.529: WARN/System.err(25804): at org.jsoup.helper.HttpConnection.get

这是方法:

 void core()
 {
     Elements links;

     final String URL = "http://dissp.com/translation/sport/footbol";
     final String YesterdayURL = "http://dissp.com/translation/sport/footbol/start";



     if (today)
     {

         Document doc = null;
         try {
             doc = Jsoup.connect(URL).get();
             links = doc.select("table[class=translation]");
             info =  links.outerHtml();



         }catch (Exception e){
             Log.e("errors","try-");
             core();
         }

     }
 }

我该如何解决?我希望应用程序始终显示结果。

4

1 回答 1

1

您收到套接字超时错误 - 您的网络或站点太慢。您是否尝试过延长超时时间?如果问题是间歇性的,这应该会有所帮助。

Jsoup.connect(url).timeout(10*1000); // sets timeout to 10 seconds

有关详细信息,请参阅 jsoup 食谱从 URL 加载文档

于 2013-03-14T23:30:55.557 回答