【问题标题】:How to get IP address through www.ipaddresslocation.org in Blackberry如何在黑莓中通过 www.ipaddresslocation.org 获取 IP 地址
【发布时间】:2011-12-15 08:32:07
【问题描述】:

我是黑莓编程的新手。 我正在尝试通过 www.ipaddressslocation.org 获取 IP 地址、县、市、州/省

我的来源:

String url = "http://www.ipaddresslocation.org/my-ip-address.php";
    // HTTP connection
    HttpConnection httpConnection = null;
    // Stream connection
    StreamConnection streamConnection = (StreamConnection) Connector
            .open(url);
    httpConnection = (HttpConnection) streamConnection;
    int code = httpConnection.getResponseCode();
    String strContent = "";
    // Check response code
    if (code == HttpConnection.HTTP_OK) {
        InputStream is = httpConnection.openInputStream();
        int length = (int) httpConnection.getLength();
        // Content is empty
        if (length != -1) {
            byte incomingData[] = new byte[length];
            is.read(incomingData);
            strContent = new String(incomingData);
            // Write content
        } else {
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                bytestream.write(ch);
            }
            strContent = new String(bytestream.toByteArray());
            bytestream.close();
        }
    }

当完成这个方法时,我得到了 strContent return 。 详细内容:

\ndocument.write('<table><tr><td>Hello, visitor from: <strong> Takatsuki, Japan</strong>');document.write('<img src=\'http://www.ipaddresslocation.org/flags/jp.png\'></td></tr>');document.write('<tr><td>Your Country Code: <b>JP</b></td></tr>');document.write('<tr><td>Your IP State: <b>Osaka</b></td></tr>');document.write('<tr><td>Your IP Address: <b>202.213.220.66</b></td></tr>');document.write('<tr><td>Your Hostname: <b>ns.isb.co.jp</b></td></tr>');document.write('<tr><td>Your ISP: <b>So-net Entertainment Corporation</b></td></tr>');document.write('<tr><td>Your Organization: <b>ISB CORP.</b></td></tr></table>');

如何从以上内容中获取IP、国家、州、城市?

谢谢和最好的问候!

【问题讨论】:

  • @HelpmetoHelpYou :您对此有何解决方案?

标签: java blackberry ip httpconnection


【解决方案1】:

由于没有标准格式,即 JSON 或 XML,您必须自己解析响应。要编写您的 scraper,只需仔细检查响应格式并设计您的算法。

例如,如果我们用“;”分割响应,则响应如下所示..

document.write('<table><tr><td>Hello, visitor from: <strong> Dhaka, Bangladesh</strong>')
document.write('<img src=\'http://www.ipaddresslocation.org/flags/bd.png\'></td></tr>')
document.write('<tr><td>Your Country Code: <b>BD</b></td></tr>')
document.write('<tr><td>Your IP State: <b>Dhaka</b></td></tr>')
document.write('<tr><td>Your IP Address: <b>116.212.105.42</b></td></tr>')
document.write('<tr><td>Your Hostname: <b>ws9-tetrasoft-dm-ac1-p16.telnet.com.bd</b></td></tr>')
document.write('<tr><td>Your ISP: <b>Telnet Communication Limited</b></td></tr>')
document.write('<tr><td>Your Organization: <b>Telnet Communication Limited</b></td></tr></table>')

然后你可以从每一行中删除“document.write('”和“')”..

那么响应就会变成

<table><tr><td>Hello, visitor from: <strong> Dhaka, Bangladesh</strong>
<img src=\'http://www.ipaddresslocation.org/flags/bd.png\'></td></tr>
<tr><td>Your Country Code: <b>BD</b></td></tr>
<tr><td>Your IP State: <b>Dhaka</b></td></tr>
<tr><td>Your IP Address: <b>116.212.105.42</b></td></tr>
<tr><td>Your Hostname: <b>ws9-tetrasoft-dm-ac1-p16.telnet.com.bd</b></td></tr>
<tr><td>Your ISP: <b>Telnet Communication Limited</b></td></tr>
<tr><td>Your Organization: <b>Telnet Communication Limited</b></td></tr></table>

现在您可以使用任何 HTML 解析器,也可以自己解析其余部分...

解决问题...

例如获取IP地址,需要解析第5行..

删除从索引 0 到索引 &lt;b&gt; + 3 的所有字符......这将从该行删除“&lt;tr&gt;&lt;td&gt;Your IP Address: &lt;b&gt;”部分...... 然后从&lt;/b&gt; 的索引中删除字符到最后...这将从该行中删除“”..您将得到“116.212.105.42”作为剩余...

【讨论】:

    【解决方案2】:

    我认为无法解析它,但我们可以使用逻辑从该字符串中获取数据 但格式应该永远相同

    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.MainScreen;
    
    public class StartUp extends UiApplication{
        public static void main(String[] args) {
            StartUp start=new StartUp();
            start.enterEventDispatcher();
        }
        public StartUp() {
            pushScreen(new simple());
        }
    }
    class simple extends MainScreen
    {
        LabelField labl;
        public simple() {
            String str="\ndocument.write('<table><tr><td>Hello, visitor from: <strong> Takatsuki, Japan</strong>');document.write('<img src=\'http://www.ipaddresslocation.org/flags/jp.png\'></td></tr>');document.write('<tr><td>Your Country Code: <b>JP</b></td></tr>');document.write('<tr><td>Your IP State: <b>Osaka</b></td></tr>');document.write('<tr><td>Your IP Address: <b>202.213.220.66</b></td></tr>');document.write('<tr><td>Your Hostname: <b>ns.isb.co.jp</b></td></tr>');document.write('<tr><td>Your ISP: <b>So-net Entertainment Corporation</b></td></tr>');document.write('<tr><td>Your Organization: <b>ISB CORP.</b></td></tr></table>');";
            str=str.substring(str.indexOf("document.write"));
            String arr[]=split(str, "document.write('");
            String s="";
            int len=arr.length;
    
            for(int i=0;i<len;i++){
                    if(i==0){
    
                        s=arr[i];
                        if(s.indexOf("<strong>")!=-1 && s.indexOf("</strong>")!=-1 ){
                            int sx=s.indexOf("<strong>")+8;
                            int dx=s.indexOf("</strong>");
                            System.out.println(s.substring(sx,dx));
                            labl=new LabelField(s.substring(sx,dx));
                            add(labl);
                        }
    
    
    
                    }else if(i==1){
                        s=arr[i];
                        if(s.indexOf("img src='")!=-1 && s.indexOf("'>")!=-1 ){
                        int sx=s.indexOf("'")+1;
                        int dx=s.indexOf("'>");
                        System.out.println(s.substring(sx,dx));
                        labl=new LabelField(s.substring(sx,dx));
                        add(labl);
                        }
                    }
                else{
                    if(arr[i].indexOf("<b>")!=-1 && arr[i].indexOf("</b>")!=-1 ){
                    int sx=arr[i].indexOf("<b>")+3;
                    int dx=arr[i].indexOf("</b>");
                    System.out.println(arr[i].substring(sx,dx));
                    labl=new LabelField(arr[i].substring(sx,dx));
                    add(labl);
    
                    }
                }
            }
        }
         public static String[] split(String strString, String strDelimiter)
            {
                int iOccurrences = 0;
                int iIndexOfInnerString = 0;
                int iIndexOfDelimiter = 0;
                int iCounter = 0;
    
                // Check for null input strings.
                if (strString == null)
                {
                    throw new NullPointerException("Input string cannot be null.");
                }
                // Check for null or empty delimiter
                // strings.
                if (strDelimiter.length() <= 0 || strDelimiter == null)
                {
                    throw new NullPointerException("Delimeter cannot be null or empty.");
                }
    
                // If strString begins with delimiter
                // then remove it in
                // order
                // to comply with the desired format.
    
                if (strString.startsWith(strDelimiter))
                {
                    strString = strString.substring(strDelimiter.length());
                }
    
                // If strString does not end with the
                // delimiter then add it
                // to the string in order to comply with
                // the desired format.
                if (!strString.endsWith(strDelimiter))
                {
                    strString += strDelimiter;
                }
    
                // Count occurrences of the delimiter in
                // the string.
                // Occurrences should be the same amount
                // of inner strings.
                while((iIndexOfDelimiter= strString.indexOf(strDelimiter,iIndexOfInnerString))!=-1)
                {
                    iOccurrences += 1;
                    iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
                }
    
                // Declare the array with the correct
                // size.
                String[] strArray = new String[iOccurrences];
    
                // Reset the indices.
                iIndexOfInnerString = 0;
                iIndexOfDelimiter = 0;
    
                // Walk across the string again and this
                // time add the
                // strings to the array.
                while((iIndexOfDelimiter= strString.indexOf(strDelimiter,iIndexOfInnerString))!=-1)
                {
    
                    // Add string to
                    // array.
                    strArray[iCounter] = strString.substring(iIndexOfInnerString, iIndexOfDelimiter);
    
                    // Increment the
                    // index to the next
                    // character after
                    // the next
                    // delimiter.
                    iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
    
                    // Inc the counter.
                    iCounter += 1;
                }
                    return strArray;
            }
    }
    

    你会得到如下图的输出

    【讨论】:

    • 哇啊啊啊啊啊啊啊!太好了......我正在开发 BB 4.5,这是编码的限制......谢谢sssssssssssssssssssssssssssssssssssssssssss
    • 我正在开发 BB 4.5,这是对编码方式的限制?你到底不支持我的逻辑?
    • 谢谢。我知道了。我的问题没有标准格式,例如 XML、JSON... 我想我必须与客户确认这一点,因为当响应格式从服务器更改时,此解决方案不正确。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多