gpt4 book ai didi

java - Android Jsoup HTML解析

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:50 26 4
gpt4 key购买 nike

我对android有点陌生,我尝试使用jsoup来解析html页面以从页面收集一些信息。我想使用名为 loadwebsite 的方法通过弹出窗口(altertbox)插入网址:

private void loadWebsite(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Inserisci url sito");

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
linkurl = input.getText().toString();
//linkurl="https://"+linkurl;
url123.setText("https://"+linkurl.toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

builder.show();



}

和一个名为 getsiteinfo() 的方法

private void getinfoWebsite(){

new Thread(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder();

try {
Document doc = Jsoup.connect(linkurl).get();
String title = doc.title();
Element image = doc.select("img").first();
String imgSrc = image.absUrl("src");
InputStream in = new java.net.URL(imgSrc).openStream();
bitmap = BitmapFactory.decodeStream(in);
builder.append(title).append("\n");
} catch (IOException e){
builder.append("Error :").append(e.getMessage()).append("\n");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
result.setText(builder.toString());
}
});
}
}).start();

}

问题是,当我尝试通过文本框传递网址时,我收到该错误:

03-26 17:22:16.826 26651-26840/it.uninsubria.pdm.htmlparsingjsoup E/AndroidRuntime: FATAL EXCEPTION: Thread-8
Process: it.uninsubria.pdm.htmlparsingjsoup, PID: 26651
java.lang.IllegalArgumentException: Must supply a valid URL
at org.jsoup.helper.Validate.notEmpty(Validate.java:102)
at org.jsoup.helper.HttpConnection.url(HttpConnection.java:72)
at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:36)
at org.jsoup.Jsoup.connect(Jsoup.java:73)
at it.uninsubria.pdm.htmlparsingjsoup.MainActivity$3.run(MainActivity.java:78)
at java.lang.Thread.run(Thread.java:764)

这里是完整的代码:

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
private Button getBtn;
private TextView result;
private ImageView img;
private Bitmap bitmap;
private Button button2;
private String linkurl = "";
private TextView url123;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
img= (ImageView) findViewById(R.id.image2);
url123 =(TextView)findViewById(R.id.url123);

getBtn =(Button) findViewById(R.id.button2); //
getBtn.setOnClickListener(new View.OnClickListener() { // Pulsante "open website
@Override //
public void onClick(View v) {
openWebsite();

}

});
getBtn =(Button) findViewById(R.id.getBtn);
getBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadWebsite();
getinfoWebsite();
img.setImageBitmap(bitmap);

}
});
}




//////////////////////////////////////////// -------------------METHODS------------------////////////////////////////////////////////

private void getinfoWebsite(){

new Thread(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder();

try {
Document doc = Jsoup.connect(linkurl).get();
String title = doc.title();
Element image = doc.select("img").first();
String imgSrc = image.absUrl("src");
InputStream in = new java.net.URL(imgSrc).openStream();
bitmap = BitmapFactory.decodeStream(in);
builder.append(title).append("\n");
} catch (IOException e){
builder.append("Error :").append(e.getMessage()).append("\n");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
result.setText(builder.toString());
}
});
}
}).start();

}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void openWebsite(){
Uri uriUrl = Uri.parse(String.valueOf(linkurl));
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//pop-up text per caricare link url
private void loadWebsite(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Inserisci url sito");

final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
linkurl = input.getText().toString();
//linkurl="https://"+linkurl;
url123.setText("https://"+linkurl.toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

builder.show();



}
}

最佳答案

该错误表明您传递给 Jsoup.connect 的参数无效(空)。看起来您创建了 onClick 监听器来设置 linkurl 的值,但您立即开始解析线程,即不等待设置 linkurl 值。例如,您可以通过在 getinfoWebsite() 内的 run 方法的开头插入以下代码来延迟代码的执行: while(linkurl.isEmpty()) { Thread.sleep(1000);}

关于java - Android Jsoup HTML解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49495129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com