【问题标题】:Eclipse Android App using JSoup, always ending within the CatchEclipse Android App 使用 JSoup,总是在 Catch 内结束
【发布时间】:2013-03-09 01:55:25
【问题描述】:

我有一些代码使用 JSoup 并在 JAVA 中成功连接到网站。

我正在尝试在 Android 上复制完全相同的内容(作为学习体验)。

我正在使用 Eclipse。

在我的 activity_main.xml 中,我有 3 个按钮和一个文本字段。

我的 JAVA 代码中没有任何错误,并确认它在 JAVA 中仍然有效(在 Netbeans 中运行)

我在 libs 文件夹中有我的 JSoup jar 〜这是一个需要一段时间才能找到的问题。

我已经放置了一些editText.setText(“Here”);看看代码在哪里。

我的文档下方有一条消息 = JSoup.connect(“http://www.Google.com”).get();

我从来没有收到过这条消息。

同样,我的 catch 例程中也有同样的信息 - 我总是进入 catch 例程,这意味着我有问题。

我已经尝试了这两种方法 - 使用 android 模拟器和通过 USB 电缆连接我的手机。我得到了相同的结果——“应用”运行良好,但显示在 catch{} 中找到的消息。

我不知所措,因为确切的代码在 Netbeans/常规 JAVA 中运行良好。

这是我的代码:

package com.example.duckriver;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.jsoup.helper.Validate;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

int counter;
Button Button1;
Button SummaryStats;
TextView display;
TextView editText;

String dataread = null;
String high = "High:";
String low = "Low:";
String filename = null;
int index = 0;
int startindex = 0;
int lastindex = 0;
int length = 0;
char[] CharArray = new char[1000];



@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);

    counter = 0;
    Button1 = (Button) findViewById(R.id.button1);
    SummaryStats = (Button) findViewById(R.id.buttonSummaryStats);
    display = (TextView) findViewById(R.id.tvMainDisplay);

    editText = (TextView) findViewById(R.id.editText);



    Button1.setOnClickListener(new View.OnClickListener()  {

        @Override
        public void onClick(View arg0) 
{
            // TODO Auto-generated method stub

            //counter++;


            Document doc;
            try{
                doc=Jsoup.connect("http://www.Google.com").get();
                editText.setText("Here");


        //get Title
                //String title = doc.title();
                //System.out.println("Title: "+title);

                //dataread = doc.body().text(); // "An example link"


                Element link = null;

            }//end try
            catch(Exception ex){
                ex.printStackTrace();
                editText.setText("Error");
                //((TextView)findViewById(R.id.tv)).setText("Error");
        }// end catch




        }
    });

    SummaryStats.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter = counter*counter;


        }
    });

    return true;
}

}

我很茫然。帮忙?

谢谢。

【问题讨论】:

  • 您在哪个版本的 Android 上进行测试?你得到什么例外?
  • 当然,您的 API 级别 >= 11,因此您可能需要修复 NetworkOnMainThreadException
  • 您是否忘记将其放入清单中?
  • 关于 Android 的版本 - 我不确定,它将是我启动 Eclipse 时设置的任何内容。我启动它并开始学习一些教程。
  • 同样,关于清单 - 我也不清楚这一点和 API 级别。我是否正确地从这些 cmets 中得出问题可能是 Eclipse 交易而不是我的代码?

标签: android eclipse jsoup


【解决方案1】:

您需要下载带有异步任务的文档,否则 Android 会抛出异常。试试这个:

@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);

    counter = 0;
    Button1 = (Button) findViewById(R.id.button1);
    SummaryStats = (Button) findViewById(R.id.buttonSummaryStats);
    display = (TextView) findViewById(R.id.tvMainDisplay);

    editText = (TextView) findViewById(R.id.editText);



    Button1.setOnClickListener(new View.OnClickListener()  {

        @Override
        public void onClick(View arg0) {
           downloadDocTask task = new downloadDocTask();
           task.execute("www.google.com");
         }
    });

    SummaryStats.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter = counter*counter;


        }
    });

    return true;
}


    private class downloadDocTask extends AsyncTask<String, Void, Document>{
        String urldisplay;
        @Override
        protected Document doInBackground(String... urls) {
            urldisplay = urls[0];
            Document doc = null;
            try {
                doc = Jsoup.connect(urldisplay).timeout(10*1000).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return doc;
            }

        @Override
        protected void onPostExecute(Document result) {
            if(result != null){
                Log.i(TAG, "downloadDocTask.onPostExcecute Document Download complete");
                buildHtml(result);
            }
            else{
                Log.i(TAG, "downloadDocTask.onPostExcecute Document == null");
            }
        }

    }


public void buildHtml(Document doc){
   // Parse document here
   String title = doc.title();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2011-12-20
    相关资源
    最近更新 更多