Reputation: 1047
I'm a newbie in Blackberry programming. I'm trying get IP address, County, city, state/province through www.ipaddressslocation.org
My source :
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();
}
}
When was finished this method, I have got the strContent return . Detail content:
\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>');
How can I get the IP , country, state, city from above content?
Thanks and best regards !
Upvotes: 1
Views: 863
Reputation: 3674
As there is no standard format i.e. JSON or XML you have to parse the response by yourself. To write your scraper just check the response format carefully and design your algorithm.
For example, the response look like the following if we split the response by ";"..
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>')
Then you can remove "document.write('" and "')" from each line..
then the response will become
<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>
Now you can use any HTML parser or parse the remaining by yourself...
just break the problem...
for example to get the IP address, you need to parse the 5th line..
remove all the characters from index 0 to the index of <b>
+ 3.... this will remove the portion "<tr><td>Your IP Address: <b>
" from that line.....
then remove the characters from the index of </b>
to the last... this will remove the "" from that line.. and you will get "116.212.105.42
" as the remaining...
Upvotes: 2
Reputation: 2862
I think it is not possible to parse it but we can use logic to get data from that string but format should same for ever
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;
}
}
you will get output as following image
Upvotes: 1