英文:
String Splitting to Capture Image URL
问题
以下是翻译好的内容:
// Code that captures the HTML String
bandName = artist.artistName;
bandName = bandName.replace(' ', '_');
try {
getUrlSource("https://en.wikipedia.org/w/api.php?action=query&titles=" + bandName + "&prop=pageimages&format=json&pithumbsize=250");
} catch (IOException e) {
e.printStackTrace();
}
// My Attempt to Parse it out
private void getUrlSource(String site) throws IOException {
URL url = new URL(site);
Log.i(TAG, "Web Address " + site);
URLConnection urlc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
a.append(inputLine);
}
Log.i(TAG, "InputLine " + a);
in.close();
String value = a.substring(a.indexOf("source\":\"") + 9,
a.indexOf("\",", a.indexOf(",", a.indexOf("source\":\"") + 9)));
Log.i(TAG, "Web Address " + value);
// webView.loadUrl(value);
}
2020-10-08 13:29:09.328 18746-18746/com.rvogl.androidaudioplayer I/TetheringManager: registerTetheringEventCallback:com.rvogl.androidaudioplayer
2020-10-08 13:29:09.346 18746-18851/com.rvogl.androidaudioplayer W/chromium: [WARNING:dns_config_service_posix.cc(341)] Failed to read DnsConfig.
2020-10-08 13:29:09.423 18746-18746/com.rvogl.androidaudioplayer I/ContentValues: Web Address https://en.wikipedia.org/w/api.php?action=query&titles=Boston&prop=pageimages&format=json&pithumbsize=250
2020-10-08 13:29:09.469 18746-18759/com.rvogl.androidaudioplayer I/roidaudioplaye: Background concurrent copying GC freed 9670(1027KB) AllocSpace objects, 3(60KB) LOS objects, 49% free, 4107KB/8214KB, paused 268us total 159.378ms
2020-10-08 13:29:09.689 18746-18746/com.rvogl.androidaudioplayer I/ContentValues: InputLine {"batchcomplete":"","query":{"pages":{"24437894":{"pageid":24437894,"ns":0,"title":"Boston","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Massachusetts_State_House_-_panoramio_(1).jpg/250px-Massachusetts_State_House_-_panoramio_(1).jpg","width":250,"height":166},"pageimage":"Massachusetts_State_House_-_panoramio_(1).jpg"}}}}
2020-10-08 13:29:09.690 18746-18746/com.rvogl.androidaudioplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rvogl.androidaudioplayer, PID: 18746
java.lang.StringIndexOutOfBoundsException: String index out of range: -17
at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:935)
at java.lang.StringBuilder.substring(StringBuilder.java:77)
at com.rvogl.androidaudioplayer.fragments.ArtistDetailsFragment.getUrlSource(ArtistDetailsFragment.java:165)
at com.rvogl.androidaudioplayer.fragments.ArtistDetailsFragment.setDetails(ArtistDetailsFragment.java:124)
at com.rvogl.androidaudioplayer.fragments.ArtistDetailsFragment.onCreateView(ArtistDetailsFragment.java:100)
任何协助将不胜感激。
英文:
I am looking to split a string similar to this: <br/>
"I/ContentValues: InputLine {"batchcomplete":"","query":{"pages":{"24437894":{"pageid":24437894,"ns":0,"title":"Boston","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Massachusetts_State_House_-_panoramio_%281%29.jpg/250px-Massachusetts_State_House_-_panoramio_%281%29.jpg","width":250,"height":166},"pageimage":"Massachusetts_State_House_-_panoramio_(1).jpg"}}}}
All I want is the hyperlink to the image:
It's easy to do it in C. By using the right(":",7) and it will parse out everything past the 7th ":". <br/>
But, I cannot seem to find anything similar in Java.
I have tried the following code without any success:
Code that captures the HTML String
bandName = artist.artistName;
bandName = bandName.replace(' ','_');
try {
getUrlSource("https://en.wikipedia.org/w/api.php?action=query&titles="+bandName+"&prop=pageimages&format=json&pithumbsize=250");
} catch (IOException e) {
e.printStackTrace();
}
My Attempt to Parse it out
private void getUrlSource(String site) throws IOException {
URL url = new URL(site);
Log.i(TAG, "Web Address " + site);
URLConnection urlc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
Log.i(TAG, "InputLine " + a);
in.close();
String value = a.substring(a.indexOf("source\":")+1,
a.indexOf("\",", a.indexOf(",")+1));
Log.i(TAG, "Web Address " + value);
//webView.loadUrl(value);
}
Logcat
2020-10-08 13:29:09.328 18746-18746/com.rvogl.androidaudioplayer I/TetheringManager: registerTetheringEventCallback:com.rvogl.androidaudioplayer
2020-10-08 13:29:09.346 18746-18851/com.rvogl.androidaudioplayer W/chromium: [WARNING:dns_config_service_posix.cc(341)] Failed to read DnsConfig.
2020-10-08 13:29:09.423 18746-18746/com.rvogl.androidaudioplayer I/ContentValues: Web Address https://en.wikipedia.org/w/api.php?action=query&titles=Boston&prop=pageimages&format=json&pithumbsize=250
2020-10-08 13:29:09.469 18746-18759/com.rvogl.androidaudioplayer I/roidaudioplaye: Background concurrent copying GC freed 9670(1027KB) AllocSpace objects, 3(60KB) LOS objects, 49% free, 4107KB/8214KB, paused 268us total 159.378ms
2020-10-08 13:29:09.689 18746-18746/com.rvogl.androidaudioplayer I/ContentValues: InputLine {"batchcomplete":"","query":{"pages":{"24437894":{"pageid":24437894,"ns":0,"title":"Boston","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Massachusetts_State_House_-_panoramio_%281%29.jpg/250px-Massachusetts_State_House_-_panoramio_%281%29.jpg","width":250,"height":166},"pageimage":"Massachusetts_State_House_-_panoramio_(1).jpg"}}}}
2020-10-08 13:29:09.690 18746-18746/com.rvogl.androidaudioplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rvogl.androidaudioplayer, PID: 18746
java.lang.StringIndexOutOfBoundsException: String index out of range: -17
at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:935)
at java.lang.StringBuilder.substring(StringBuilder.java:77)
at com.rvogl.androidaudioplayer.fragments.ArtistDetailsFragment.getUrlSource(ArtistDetailsFragment.java:165)
at com.rvogl.androidaudioplayer.fragments.ArtistDetailsFragment.setDetails(ArtistDetailsFragment.java:124)
at com.rvogl.androidaudioplayer.fragments.ArtistDetailsFragment.onCreateView(ArtistDetailsFragment.java:100)
Any assistance would be greatly appreciated
答案1
得分: 1
以下是翻译好的代码部分:
public static StringBuilder cutImg(StringBuilder split){
int start = split.indexOf("\"source\":\"") + new String("\"source\":\"").length();
split.delete(0, start);
split.delete(split.indexOf(","), split.length());
return split;
}
还有许多方法可以实现这个,我仍然不太理解完整的URL语法,所以如果语法不同可能会抛出异常。
英文:
In the part you use the substring I use a method like this.
public static StringBuilder cutImg(StringBuilder split){
int start=split.indexOf("\"source\":")+new String("\"source\":").length();
split.delete(0, start);
split.delete(split.indexOf(","), split.length());
return split;
}
There are many methods to do it and I still don't understad the complete syntax of the URL so it can throw you exceptions if the syntax is different.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论