Java 如果忽略发生了,返回其他内容?
正在从Jsoup Connect获取文档。 但是如果Jsoup.connect出现错误。我想 jgurl,并继续运行代码。 不可能吗? (此代码将从外部多次调用,但如果发生一个异常,则会停止) 调用此URL图片的外部源Java 如果忽略发生了,返回其他内容?,java,jsoup,Java,Jsoup,正在从Jsoup Connect获取文档。 但是如果Jsoup.connect出现错误。我想 jgurl,并继续运行代码。 不可能吗? (此代码将从外部多次调用,但如果发生一个异常,则会停止) 调用此URL图片的外部源 package utils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.springframework.beans.factory.annotation.Autowired; impo
package utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Urlpicture {
public String get_main_url(int num) {
String jpgurl = null;
try{
String url = "https://www.zigbang.com/items1/"+num;
jpgurl = "http://z1.zigbang.com/items/7697786/538ec7a16033f0c32eac29ac50deb04a1e6bf3ba.jpg?h=800&q=60";
Document doc = Jsoup.connect(url).get(); // this part require try catch
jpgurl = doc.select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
System.out.println(url);
System.out.println(jpgurl);
}catch(Exception e){
e.printStackTrace();
}
return jpgurl;
}
}
=====================================================================
修复后,它会在异常后继续运行。
但是,即使url链接存在并带有有效的数字
它将所有值作为异常返回
@Service
public class Urlpicture {
public String get_main_url(String url) {
Document doc;
String jpgurl = null;
try{
jpgurl = "http://z1.zigbang.com/items/7697786/538ec7a16033f0c32eac29ac50deb04a1e6bf3ba.jpg?h=800&q=60";
jpgurl = Jsoup.connect(url).get().select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
}catch(Exception e){
e.printStackTrace();
}finally{
return jpgurl;
}
我不知道为什么这会一直为所有值抛出异常问题在于
jgurl
变量的范围,您需要在try
块之前声明该变量,如下所示
另外,要解决第二个问题,您需要将下载图像逻辑提取到一个单独的方法,并在给定的num
找不到图像时,使用默认的num
再次调用它,如下所示:
7666495
https://www.zigbang.com/items1/7666495// this link is valid but throw exception
No=
java.lang.NullPointerException
at controller.main_controller.searchHandler(main_controller.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springf
将jgurl的声明从try部分移动,否则其范围仅限于它,并且在catch中无法访问它。您可以在try之前声明它,给它默认值(错误消息),如果try部分将顺利执行,它也会将jgurl更新为正确的值。我更改了我的代码。@manjoojo如果它更改了您的question@manjoojo:您能同时添加“外部”(调用方)代码吗?现在有什么问题,你能解释一下吗?好的,这个get_main_url()函数是从外部调用的。我正在输入多个号码,并从某个网络城市获得jpg。你也可以添加“外部”(呼叫者)代码吗?但如果输入了错误的号码。Document doc=Jsoup.connect(url.get();引发空点异常的部分。Document doc=Jsoup.connect(url.get();此JSOUP部件需要try catch
@Service
public class Urlpicture {
public String get_main_url(String url) {
Document doc;
String jpgurl = null;
try{
jpgurl = "http://z1.zigbang.com/items/7697786/538ec7a16033f0c32eac29ac50deb04a1e6bf3ba.jpg?h=800&q=60";
jpgurl = Jsoup.connect(url).get().select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
}catch(Exception e){
e.printStackTrace();
}finally{
return jpgurl;
}
7666495
https://www.zigbang.com/items1/7666495// this link is valid but throw exception
No=
java.lang.NullPointerException
at controller.main_controller.searchHandler(main_controller.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springf
public String get_main_url(int num) {
int num = 7453201;
String jpgurl = null;
try {
downloadImage(num);
} catch (Exception e) {
e.printStackTrace();
num=0;//set the default value for which img exists
downloadImage(num);//call downloadImage with default num
}
}
private String downloadImage(int num) {
String url = "https://www.zigbang.com/items1/"+num;
System.out.println(url);
Document doc = Jsoup.connect(url).get();
jpgurl = doc.select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
System.out.println(i + "="+jpgurl);
}