我正在使用Jsoup,在Android中,我想从这个网站的第一个标签“img”获取属性“src”:http://www.proyectoglass.com/feeds/posts/default?alt=rss,由于某些原因我无法计算程序正在停止,我很无能为力。这是代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView test=(TextView) findViewById(R.id.testText);
try {
Document doc = Jsoup.connect("http://www.proyectoglass.com/feeds/posts/default?alt=rss").ignoreContentType(true).get();
Element img = doc.select("img").first();
test.setText(""+img.attr("abs:src"));
} catch (IOException e) {
test.setText(e.toString());
}
}
这是我的LogCat:
06-14 15:57:48.064: W/dalvikvm(542): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-14 15:57:48.106: E/AndroidRuntime(542): FATAL EXCEPTION: main
06-14 15:57:48.106: E/AndroidRuntime(542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsoup_test/com.example.jsoup_test.MainActivity}: java.lang.NullPointerException
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.os.Looper.loop(Looper.java:123)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-14 15:57:48.106: E/AndroidRuntime(542): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 15:57:48.106: E/AndroidRuntime(542): at java.lang.reflect.Method.invoke(Method.java:521)
06-14 15:57:48.106: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-14 15:57:48.106: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-14 15:57:48.106: E/AndroidRuntime(542): at dalvik.system.NativeStart.main(Native Method)
06-14 15:57:48.106: E/AndroidRuntime(542): Caused by: java.lang.NullPointerException
06-14 15:57:48.106: E/AndroidRuntime(542): at com.example.jsoup_test.MainActivity.onCreate(MainActivity.java:28)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-14 15:57:48.106: E/AndroidRuntime(542): ... 11 more
在网络上,我想从src获取网址。
<img width="640" height="344" border="0" src="http://4.bp.blogspot.com/-tNEIyl1tTJA/UbRVa2bKpiI/AAAAAAAAAs4/xqW9VPw7b9g/s640/Larry-Page-Google-Project-Glass.jpg"/>
答案 0 :(得分:0)
谁能告诉我我做错了什么?
在MainActivity.java第28行的对象引用上调用方法时,你做了一件坏事。
答案 1 :(得分:0)
您的链接看起来像rss feed(http://www.proyectoglass.com/feeds/posts/default?alt=rss)。
任何方式假设你有html as并使用asynctask
<img width="640" height="344" border="0" src="http://4.bp.blogspot.com/-tNEIyl1tTJA/UbRVa2bKpiI/AAAAAAAAAs4/xqW9VPw7b9g/s640/Larry-Page-Google-Project-Glass.jpg"/>
您可以执行以下操作
String html ="<img width="+640+" height="+344+" border="+0+" src="+"http://4.bp.blogspot.com/-tNEIyl1tTJA/UbRVa2bKpiI/AAAAAAAAAs4/xqW9VPw7b9g/s640/Larry-Page-Google-Project-Glass.jpg"+"/>";
Document doc = Jsoup.parse(html);
Element link = doc.select("img").first();
String s = link.attr("src");
编辑:
public class asynctask extends Activity {
TextView tv;
ArrayList<String> bb = new ArrayList<String>();
ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
pd = new ProgressDialog(asynctask.this);
pd.setMessage("Loading..");
new TheTask().execute();
}
class TheTask extends AsyncTask <Void,Void,Void>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
try
{
URL url = new URL("http://www.proyectoglass.com/feeds/posts/default?alt=rss");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(url.openConnection().getInputStream(), "UTF_8");
//xpp.setInput(getInputStream(url), "UTF-8");
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
{
}
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
{
}
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem)
bb.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Document doc = Jsoup.parse(bb.get(0).toString());
Element link = doc.select("img").first();
String s = link.attr("src");
pd.dismiss();
tv.setText(s);
}
}
}
activity_main.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:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
快照