Get external IP in Java - Android Studio

Asked

Viewed 345 times

0

I want to get my external IP by clicking 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);
}
  • Please see which error is shown in your terminal and post here

3 answers

0

Put internet access in your xml file

<uses-permission android:name="android.permission.INTERNET"/>
  • It is already added and I continue with the same error. I added in AndroidManifest.xml, that is correct?

  • This is correct. I also wanted to do a function like this and could not. I will post the solution that I found as an answer.

0


For the purpose of solution, I have done as follows:

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);
}

0

I made a solution to check if the client uses proxy, before it was 2 methods, one to get the ip and one 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 way works with me quiet, this is to check if the client is proxy, but if you give an adapted you can only return the IP string.

  • I guess because I’m too beginner, I couldn’t implement the code...

  • Oops, so am I. So look for Volley and how to get String Sponse. You’ll get very simple examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.