Jsoup:Android App崩溃了

时间:2017-02-02 15:06:08

标签: java android html jsoup

我有以下代码:

String webPage = "http://www.something.com";
String html = null;

try{
      html = Jsoup.connect(webPage).get().html();
} catch (Exception e) {
      e.printStackTrace();
}
Toast.makeText(this, html, Toast.LENGTH_SHORT).show();

如果异常Exception代码执行其他操作(在此代码之前)并且不执行try{}中的代码,如果异常为IOException,则应用程序崩溃< / p>

2 个答案:

答案 0 :(得分:1)

应用因NetworkOnMainThreadException

而崩溃

不允许您从主线程执行网络操作。

相反,请尝试从AsyncTask

运行Jsoup

答案 1 :(得分:0)

此外,你应该阅读什么样的Exception Jsoup可以抛出并处理它们,而不是捕获基本的jave Exception类。如果您计划以相同的方式处理它们,也可以在一个catch块中捕获多个异常。

示例:

class OrderConfirmation extends Notification
{
    use Queueable;

    public $order;
    public $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user , $order)
    {
        $this->order = $order;
        $this->user = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Order Confirmation ['.$this->order->ID.']')
            ->greeting('Hi '.$this->user->F_NAME)
            ->line('Thank you for ordering from Awesome Store.')
            ->line('Once Payment has been confirmed we will send you a Payment Confirmation Email')
            ->line('As your order progresses we will send you updates. You can also track your order status by viewing it on our site at anytime')
            ->action('View Order', 'order url here')
            ->line('Thank you for shopping with us!');
           /**** What do i use here to pass $this->order to the view ????****/

    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }