Get external IP in Java - Android Studio

0

I want to get my external IP when I click a button in my app, but every time I click the button, the app closes itself. How can I do that? My code:

public void onClickBtnAtualizar(View view) throws Exception {
    TextView txtResposta = (TextView) findViewById(R.id.txtResposta);
    txtResposta.setText(obterIP());
}

public String obterIP() {
    URL page = new URL("https://diagnostic.opendns.com/myip");
    BufferedReader in = new BufferedReader(new InputStreamReader(
            page.openStream()));

    String ip = in.readLine();
    return(ip);
}
    
asked by anonymous 20.11.2018 / 18:52

3 answers

0

For solution purposes, I did this:

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;

public void onClickBtnAtualizar(View view) {
    PrimeThread p = new PrimeThread(143);
    p.start();
}

class PrimeThread extends Thread {
    long minPrime;
    PrimeThread(long minPrime) {
        this.minPrime = minPrime;
    }

    public void run() {
        obterIP();
    }
}

public void obterIP() {
    Document doc;
    String ip = null;
    try {
        doc = Jsoup.connect("https://diagnostic.opendns.com/myip").get();
        ip = doc.body().toString();
    } catch (IOException e) {
        e.printStackTrace();
    }

    TextView txtResposta = (TextView) findViewById(R.id.txtResposta);
    txtResposta.setText(ip);
}
    
23.11.2018 / 18:06
0

Put internet access in your xml file

<uses-permission android:name="android.permission.INTERNET"/>
    
20.11.2018 / 19:24
0

I made a solution to check if the client uses proxy, before it was 2 methods, one to get the ip and another to test it. This way you already do both.

   RequestQueue queue;
   StringRequest proxyRequest, ipRequest;
  String url = null;


    queue = Volley.newRequestQueue(this);
            String url_ip = "https://api.ipify.org";
            ipRequest = new StringRequest(Request.Method.GET, url_ip,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            url = "http://check.getipintel.net/check.php?ip="+
                                    response+"&contact="+user.getEmail()+"&flags=b";
                            proxyRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    float re = 0f;
                                    try {
                                        re = Float.valueOf(response);
                                    } catch (NumberFormatException e){
                                        Crashlytics.logException(e);
                                        re = 0;
                                    }
                                    if(re > 0.995){
                                        Toast.makeText(getApplicationContext(), "Proxy/VPN/Bots não são permitidos no app!", Toast.LENGTH_SHORT).show();
                                    }
                                    else {
                                        Intent i = new Intent(getApplicationContext(), MainActivity.class);
                                        startActivity(i);
                                        finish();
                                        progressBar.setVisibility(View.GONE);
                                    }
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                     Toast.makeText(getApplicationContext(), "Erro: " + error.getMessage(), Toast.LENGTH_SHORT).show();
                                        progressBar.setVisibility(View.GONE);
                                        Intent in = new Intent(getApplicationContext(), SplashActivity.class);
                                        startActivity(in);
                                        finish();
                                       }
                            });
                            queue.add(proxyRequest);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Erro: " + error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

     queue.add(ipRequest);

This is how it works with me, this is to check if the client is a proxy, but if you give an adapted one, you can return only the IP String.

    
20.11.2018 / 21:29