使用android studio,Java从网站解析Html

时间:2015-06-04 05:45:50

标签: java android

我今天提出这个问题是因为我找不到关于Google和/或Stack Overflow的某些信息。我的问题是,我正在创建一个Android应用程序,我想从网站获得某些信息。所以我使用Jsoup作为我的解析库。我按照所有说明并设置文本中的数据以显示HTML文本。出于某种原因,它是网络连接还是我的代码与网站通信错误?我不知道该应用程序是什么。我正在使用Android Studio,因此在运行模拟器或设备时,它不会从网站上提取HTML。

这是我的2个类的代码。这必须是Java。

package update.app.jdog1218.com.messingaround;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends Activity {

private Button answer_Button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout bible = new RelativeLayout(this);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.


    return super.onOptionsItemSelected(item);
}

public void gotoInbeddedBible(MenuItem item) {
    Intent bibleclass = new Intent(this, bible.class);

    final int result = 1;

    bibleclass.putExtra("callingActivity", "MainActivity");

    startActivity(bibleclass);
    startActivityForResult(bibleclass, result);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

}

public void exit(MenuItem item) {
    Button exitProcess = (Button) findViewById(R.id.submit);
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);
}

public void sotd(MenuItem item) {
    Intent clickedSotd = new Intent(this, Sotd.class);

    int result = 1;

    startActivity(clickedSotd);

}
}

这是我想要使用Jsoup的类。

package update.app.jdog1218.com.messingaround;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.PersistableBundle;
 import android.view.Menu;
 import android.widget.TextView;

 import org.jsoup.Jsoup;
 import org.jsoup.Connection;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
 import org.w3c.dom.Node;
 import org.w3c.dom.Text;

 import java.io.IOException;

 import static org.jsoup.Jsoup.*;
 /*
 * Created by Joel on 5/28/2015.
 */
 public class Sotd extends Activity {


@Override
protected void onStart() {
    super.onStart();
}
String title;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.sotd);
    Intent activityThatCalled = getIntent();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return super.onCreateOptionsMenu(menu);
}

/**
 * Pull and Parse the HTML of Compass HB for SOTD.
 *
 * @return DocumentString
 * @throws IOException
 */
protected String pullHTML() {
    super.onStart();
    final String url = "https://www.compasshb.com/api/v1/passages";
    Document doc = null;
    TextView textView = (TextView) findViewById(R.id.SOTD);
    try {
        doc = Jsoup.connect(url).get();
        textView.setText(doc.ownText());
        String paragraph = doc.title();


        return paragraph;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;

}

@Override
protected void onPostResume() {
    super.onPostResume();
}
}

这是我想要输出的布局的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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:weightSum="1"
android:background="@drawable/the_rendered_background">

<TextView
    android:text="@string/text_of_activity_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:height="40dp"
    android:capitalize="none"
    android:width="200dp"
    android:textColor="#000000"
    android:id="@+id/text_view" />


</LinearLayout>

SOTD也是我想要输出的地方。

<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Sotd"
android:weightSum="1"
android:background="@drawable/the_rendered_background"
android:backgroundTintMode="add">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/SOTD"
    />
</LinearLayout>

非常多我只需要有人告诉我我做错了什么./Review

0 个答案:

没有答案