D
D
Denius (genius)2018-09-25 14:11:57
Java
Denius (genius), 2018-09-25 14:11:57

Can't parse with JSOUP, what's wrong?

App closes immediately

package com.example.denius.raspisanieparser;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends Activity {

    public Elements title;
    public ArrayList<String> titleList = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.spisok);
        new NewThread().execute();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, R.id.product_name, titleList);
    }

    public class NewThread extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... arg) {
            Document doc;
            try {
                doc = Jsoup.connect("http://example.com/").get();
                title = doc.select("h1");
                titleList.clear();
                for (Element titles : title) {
                    titleList.add(titles.text());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            lv.setAdapter(adapter);
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/spisok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"></ListView>

</android.support.constraint.ConstraintLayout>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
argen6666, 2018-09-25
@argen6666

Most likely you forgot to add Internet permission to the manifest

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question