字符串分割以捕获图像网址

huangapple go评论65阅读模式
英文:

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/>

&quot;I/ContentValues: InputLine {&quot;batchcomplete&quot;:&quot;&quot;,&quot;query&quot;:{&quot;pages&quot;:{&quot;24437894&quot;:{&quot;pageid&quot;:24437894,&quot;ns&quot;:0,&quot;title&quot;:&quot;Boston&quot;,&quot;thumbnail&quot;:{&quot;source&quot;:&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Massachusetts_State_House_-_panoramio_%281%29.jpg/250px-Massachusetts_State_House_-_panoramio_%281%29.jpg&quot;,&quot;width&quot;:250,&quot;height&quot;:166},&quot;pageimage&quot;:&quot;Massachusetts_State_House_-_panoramio_(1).jpg&quot;}}}}

All I want is the hyperlink to the image:

> https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Massachusetts_State_House_-panoramio%281%29.jpg/250px-Massachusetts_State_House_-panoramio%281%29.jpg

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(&#39; &#39;,&#39;_&#39;);
        try {
            getUrlSource(&quot;https://en.wikipedia.org/w/api.php?action=query&amp;titles=&quot;+bandName+&quot;&amp;prop=pageimages&amp;format=json&amp;pithumbsize=250&quot;);
        } 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, &quot;Web Address &quot; + site);
        URLConnection urlc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                urlc.getInputStream(), &quot;UTF-8&quot;));
        String inputLine;

        StringBuilder a = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
        

a.append(inputLine);
    Log.i(TAG, &quot;InputLine &quot; + a);
    in.close();
    String value = a.substring(a.indexOf(&quot;source\&quot;:&quot;)+1,
            a.indexOf(&quot;\&quot;,&quot;, a.indexOf(&quot;,&quot;)+1));
    Log.i(TAG, &quot;Web Address &quot; + 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&amp;titles=Boston&amp;prop=pageimages&amp;format=json&amp;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 {&quot;batchcomplete&quot;:&quot;&quot;,&quot;query&quot;:{&quot;pages&quot;:{&quot;24437894&quot;:{&quot;pageid&quot;:24437894,&quot;ns&quot;:0,&quot;title&quot;:&quot;Boston&quot;,&quot;thumbnail&quot;:{&quot;source&quot;:&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Massachusetts_State_House_-_panoramio_%281%29.jpg/250px-Massachusetts_State_House_-_panoramio_%281%29.jpg&quot;,&quot;width&quot;:250,&quot;height&quot;:166},&quot;pageimage&quot;:&quot;Massachusetts_State_House_-_panoramio_(1).jpg&quot;}}}}
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(&quot;\&quot;source\&quot;:&quot;)+new String(&quot;\&quot;source\&quot;:&quot;).length();
        split.delete(0, start);
        split.delete(split.indexOf(&quot;,&quot;), 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.

huangapple
  • 本文由 发表于 2020年10月9日 08:05:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64272232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定