2011-12-15 99 views
1

我是黑莓编程的新手。 我试图获取IP地址,县,城市,州/通过www.ipaddressslocation.org省如何通过黑莓的www.ipaddresslocation.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。 详细内容:

\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地址,国家,州,城市?

谢谢,最好的问候!

+0

我们必须用我们自己的逻辑 – 2011-12-15 08:57:34

+0

@HelpmetoHelpYou:什么是你有关的解决方案? – Tama 2011-12-15 09:16:55

回答

2

由于没有标准格式,即JSON或XML,您必须自行解析响应。要编写你的报废者,请仔细检查响应格式并设计你的算法。

例如,响应如下所示,如果我们分裂由响应;从每一行..

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>') 

然后,可以移除“文件撰写(“”和“”)”“” ..

则响应会变得

<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删除所有字符的<b> + 3的指标.. ..这将从该行删除部分“<tr><td>Your IP Address: <b>”..... 然后将字符从</b>的索引中删除到最后...这将从该行中删除“”,您将获得“ 116.212.105.42“作为其余...

+0

很棒!!!!!!!!!!非常感谢你....爱你^^ – Tama 2011-12-15 10:06:00

1

我认为这是不可能的解析它,但我们可以使用逻辑从该字符串中获取数据 但格式应该永远一样

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; 
     } 
} 

你会得到输出如下图像Image