哈哈,这个说白了就是手机后台访问网站,再解析获得的数据
app: http://fir.im/dpxu 可以下载看看效果
书接上文:http://blog.csdn.net/i_do_can/article/details/50421882
android手机联网,所以开启一个异步任务执行后台程序
public class IpAddressInfo extends AsyncTask<String, Integer, StringBuffer>现在推荐的一种手机访问网络获取网页数据的方式是:URLConnection
给出两种方式联网的函数
public void doGetURL(String url) throws Exception { URL localURL = new URL(url); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) { tempHTML.append(tempLine); } } private void doPostURL(String url) throws Exception{ URL localURL = new URL(url); URLConnection connection = localURL.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection)connection; httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8"); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(url.length())); outputStream = httpURLConnection.getOutputStream(); outputStreamWriter = new OutputStreamWriter(outputStream); outputStreamWriter.write(url.toString()); outputStreamWriter.flush(); if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(inputStreamReader); while ((tempLine = reader.readLine()) != null) { tempHTML.append(tempLine); } }
这个网页很奇怪,他的IP 我一直 获取不出来,只能得到IP的其他相关信息,而且网址也不是这个,我给出他的那个网址:
这只是我个人的获取,如果涉及到相关利益,我表示很抱歉,希望大家不要把这个网址商业使用
IP的获取淘宝有个貌似高大上的方式,就是极度不稳定:http://ip.taobao.com/service/getIpInfo2.php?ip=myip
之所以说看着”高大上“,是因为返回的是JSON串,需要用JSONObject解析,加上”貌似“是因为太不稳定啦
后台调用的代码:
public static String GetNetIp2() throws Exception{ String IP = ""; String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip"; URL url = new URL(address); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setUseCaches(false); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream in = connection.getInputStream(); // 将流转化为字符串 BufferedReader reader = new BufferedReader( new InputStreamReader(in)); String tmpString = ""; StringBuilder retJSON = new StringBuilder(); while ((tmpString = reader.readLine()) != null) { retJSON.append(tmpString + "\n"); } JSONObject jsonObject = new JSONObject(retJSON.toString()); String code = jsonObject.getString("code"); if (code.equals("0")) { JSONObject data = jsonObject.getJSONObject("data"); IP = data.getString("ip") + "(" + data.getString("country") + data.getString("area") + "区" + data.getString("region") + data.getString("city") + data.getString("isp") + ")"; } else { IP = ""; } } else { IP = ""; } return IP; }
获取IP就极其简单啦
<span style="white-space:pre"> </span>line = null; // 从反馈的结果中提取出IP地址 int start = strber.indexOf("["); int end = strber.indexOf("]" ); line =strber.substring(start + 1 ,end);
<span style="white-space:pre"> </span>/** * onPostExecute方法用于在执行完后台任务后更新UI,显示结果 EditText */ @Override protected void onPostExecute(StringBuffer result) { // 判断是否为null,若不为null,则在页面显示HTML代码 if (result != null) { <span style="white-space:pre"> </span>edtHTTP.setText(result); } super.onPostExecute(result); }OK ,基本完成啦
源码下载:http://download.csdn.net/detail/i_do_can/9381118