我是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返回。 详细内容:
\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 :(得分: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>')
然后你可以从每一行中删除“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
”作为剩余...
答案 1 :(得分: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;
}
}
您将获得如下图像
的输出