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 {@link android.os.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 { @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; } }