Android 使用Jsoup连接时出现SecurityException
我制作了一个小的安卓应用程序,用我的应用程序在谷歌图片上搜索图像。 最初,我测试在Android 使用Jsoup连接时出现SecurityException,android,jsoup,Android,Jsoup,我制作了一个小的安卓应用程序,用我的应用程序在谷歌图片上搜索图像。 最初,我测试在TextView中打印图像src。 但该应用程序会引发一个securityexception,从而停止运行 package com.example.googleimages; import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; i
TextView
中打印图像src
。
但该应用程序会引发一个securityexception
,从而停止运行
package com.example.googleimages;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
ProgressBar progress;
Button btn;
EditText searchtext;
Document doc;
TextView tv;
String src;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
btn.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
progress=(ProgressBar)findViewById(R.id.progressBar1);
btn=(Button)findViewById(R.id.search);
searchtext=(EditText)findViewById(R.id.searchText);
tv=(TextView)findViewById(R.id.textView1);
}
@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);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
progress.setVisibility(0);
for(int i=0;i<100;i+=20)
progress.setProgress(i);
Thread browser=new Thread(){
public void run(){
try {
doc=Jsoup.connect("http://www.google.com.my/imghp?hl=en&tab=wi").data("q",searchtext.getText().toString())
.data("btnG","Search by image").userAgent("Mozilla").get();
Elements elements=doc.getElementsByTag("img");
for(Element e:elements){
src=e.attr("src");
tv.setText(tv.getText()+src+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
browser.start();
}
}
package com.example.googleimages;
导入java.io.IOException;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入org.jsoup.select.Elements;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ProgressBar;
导入android.widget.TextView;
公共类MainActivity扩展活动实现OnClickListener{
进度条进度;
按钮btn;
编辑文本搜索文本;
文件文件;
文本视图电视;
字符串src;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
初始化();
btn.setOnClickListener(此);
}
私有void初始化(){
//TODO自动生成的方法存根
进度=(ProgressBar)findViewById(R.id.progressBar1);
btn=(按钮)findViewById(R.id.search);
searchtext=(EditText)findViewById(R.id.searchtext);
tv=(TextView)findViewById(R.id.textView1);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
进度。设置可见性(0);
对于(int i=0;i您是否在清单中添加了internet权限?@Raghunandan:好的,我定义了它,它解决了问题,但现在图像的源url没有显示在textview中,而是在几秒钟后应用程序崩溃。然后jsoup解析出现问题。检查它,您应该能够显示url