Android Open Source - PortAuthority Get External Ip Async Task






From Project

Back to project page PortAuthority.

License

The source code is released under:

GNU General Public License

If you think the Android project PortAuthority listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.aaronjwood.portauthority.async;
//  w ww  . ja va 2 s.  c o m
import android.os.AsyncTask;
import android.util.Log;

import com.aaronjwood.portauthority.response.MainAsyncResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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 java.io.IOException;

public class GetExternalIpAsyncTask extends AsyncTask<Void, Void, String> {

    private static final String TAG = "GetExternalIpAsyncTask";
    private static final String EXTERNAL_IP_SERVICE = "http://whatismyip.akamai.com/";
    private MainAsyncResponse delegate;

    public GetExternalIpAsyncTask(MainAsyncResponse delegate) {
        this.delegate = delegate;
    }

    @Override
    protected String doInBackground(Void... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(EXTERNAL_IP_SERVICE);
        HttpResponse response = null;

        try {
            response = httpclient.execute(httpget);
        }
        catch(ClientProtocolException e) {
            Log.e(TAG, e.getMessage());
            return "Couldn't get your external IP";
        }
        catch(IOException e) {
            Log.e(TAG, e.getMessage());
            return "Couldn't get your external IP";
        }

        String ip = null;
        HttpEntity entity = response.getEntity();

        try {
            ip = EntityUtils.toString(entity);
        }
        catch(ParseException e) {
            Log.e(TAG, e.getMessage());
            return "Couldn't get your external IP";
        }
        catch(IOException e) {
            Log.e(TAG, e.getMessage());
            return "Couldn't get your external IP";
        }

        return ip;
    }

    @Override
    protected void onPostExecute(String result) {
        delegate.processFinish(result);
    }
}




Java Source Code List

com.aaronjwood.portauthority.activity.HostActivity.java
com.aaronjwood.portauthority.activity.MainActivity.java
com.aaronjwood.portauthority.async.GetExternalIpAsyncTask.java
com.aaronjwood.portauthority.async.GetHostnameAsyncTask.java
com.aaronjwood.portauthority.async.ScanHostsAsyncTask.java
com.aaronjwood.portauthority.async.ScanPortsAsyncTask.java
com.aaronjwood.portauthority.network.Discovery.java
com.aaronjwood.portauthority.network.Host.java
com.aaronjwood.portauthority.network.Wireless.java
com.aaronjwood.portauthority.response.HostAsyncResponse.java
com.aaronjwood.portauthority.response.MainAsyncResponse.java
com.aaronjwood.portauthority.runnable.ScanHostsRunnable.java
com.aaronjwood.portauthority.runnable.ScanPortsRunnable.java