123456789101112131415161718192021222324252627282930313233343536373839 |
- package de.tudarmstadt.informatik.hostage.commons;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONObject;
- import android.os.AsyncTask;
- /**
- * A AsyncTask that determines and returns the external IP address of the device.
- * Therefore it uses a given URL to get a JSON Object containing the external IP address.
- * @author Lars Pandikow
- *
- */
- public class GetExternalIPTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... url) {
- String ipAddress = null;
- try {
- HttpClient httpclient = new DefaultHttpClient();
- HttpGet httpget = new HttpGet(url[0]);
- HttpResponse response;
- response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
- entity.getContentLength();
- String str = EntityUtils.toString(entity);
- JSONObject json_data = new JSONObject(str);
- ipAddress = json_data.getString("ip");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return ipAddress;
- }
- }
|