private
class
getPublicIP
extends
AsyncTask<String, Void, Message> {
@Override
protected
Message doInBackground(String... params) {
StringBuffer sBuffer =
new
StringBuffer();
Message msg = m_uiHandler.obtainMessage();
try
{
String urlAddr = params[
0
];
URL url =
new
URL(urlAddr);
URLConnection conn = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)conn;
httpURLConnection.setRequestMethod(
"GET"
);
if
(httpURLConnection !=
null
){
httpURLConnection.setConnectTimeout(
10000
);
httpURLConnection.setRequestProperty(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
if
(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
InputStreamReader isr =
new
InputStreamReader(conn.getInputStream());
BufferedReader br =
new
BufferedReader(isr);
while
(
true
){
String line = br.readLine();
if
(line==
null
){
break
;
}
sBuffer.append(line);
}
br.close();
httpURLConnection.disconnect();
}
}
String publicIP = sBuffer.toString();
if
(publicIP !=
null
&& !publicIP.isEmpty()) {
LogTrace.i(TAG,
"PUBLIC IP == "
+ publicIP);
msg.what = GET_PUBLIC_IP;
msg.obj = publicIP;
}
else
{
msg.what = GET_PUBLIC_IP_FAIL;
}
}
catch
(Exception e) {
LogTrace.e(TAG,
"URL Exception = "
+ e.getMessage());
msg.what = GET_PUBLIC_IP_EXCEPTION;
}
return
msg;
}