我是Blackberry编程的新手。
我正在尝试通过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,国家,州,城市?
谢谢,最好的问候!
最佳答案
由于没有标准格式,即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>')
然后,您可以从每一行中删除“ 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到
<b>
+ 3的索引中删除所有字符。这将从该行中删除“ <tr><td>Your IP Address: <b>
”部分。然后从
</b>
的索引中删除字符到最后一个...这将从该行中删除“。”,然后您将获得“ 116.212.105.42
”作为其余字符...