0

I want to use IP2Location.io in my Android Studio app. I am starting with this program from their internet site. I am stuck on this error. I do not understand why this difficulty including java/net/http/HttpRequest. I researched many similar threads on Stackoverflow but did not find a resolution. I tried unsuccessfully to include other HttpRequest. Perhaps I do not know how to do that. Any ideas will be appreciated.

I started at... https://blog.ip2location.com/knowledge-base/using-ip2location-io-java-sdk-in-a-maven-project/ Using IP2Location.io Java SDK in a Maven project

I scrolled down to... Adding the IP2Location.io Java SDK into the Maven dependency

I worked thru to the end, adding the code to a new Android Studio "Empty Activity". I made changes as I went along to get rid of the "red errors" :) It took me most of a day. My last, most difficult error was the infamous red R. The program builds cleanly on Android studio. But here is the result..

E/AndroidRuntime: FATAL EXCEPTION: main
Process: gmd.com.mickwebsite.ip2locationion, PID: 1505
**java.lang.NoClassDefFoundError: Failed resolution of: Ljava/net/http/HttpRequest;**
    at com.ip2location.IPGeolocation.Lookup(IPGeolocation.java:59)
    at com.ip2location.IPGeolocation.Lookup(IPGeolocation.java:46)
    at gmd.com.mickwebsite.ip2locationion.MainActivity.onCreate(MainActivity.java:30)

... Here is the complete code... Note this about half way: // <<--App hangs up here

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.JsonObject;
import com.ip2location.Configuration;
import com.ip2location.DomainWhois;
import com.ip2location.IPGeolocation;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView mainDisplay = findViewById(R.id.mainDisplay);

        // Configures IP2Location.io API key
        Configuration config = new Configuration();
//      String apiKey = "API KEY";  //  I used my personal API key
        config.setApiKey(apiKey);
        IPGeolocation ipl = new IPGeolocation(config);
        // Lookup ip address geolocation data

        JsonObject myObj;
        // the language parameter is only available for Plus and Security plans
        try {
            myObj = ipl.Lookup("8.8.8.8");  **// <<--App hangs up here**
            // java.lang.NoClassDefFoundError:
            // Failed resolution of: Ljava/net/http/HttpRequest;
        } catch (Exception e) {
            mainDisplay.setText(String.valueOf(e));
            throw new RuntimeException(e);
        }
        mainDisplay.setText(String.valueOf(myObj));
// I added the id for the textView in the xml
        DomainWhois whois = new DomainWhois(config);
        // Lookup domain information
        JsonObject myObj2;
        try {
            myObj2 = whois.Lookup("locaproxy.com");
        } catch (Exception e) {
            mainDisplay.setText(String.valueOf(e));
            throw new RuntimeException(e);
        }
        mainDisplay.setText(String.valueOf(myObj2));
    }   //  protected void onCreate
}   // public class MainActivity
Mick
  • 663
  • 1
  • 9
  • 18
  • That blog post is for desktop or server Java, not Android. – CommonsWare Jun 20 '23 at 22:41
  • Thank you. I know that. I have been hoping that it might be workable on Android. Is that a hopeless hope? :) – Mick Jun 20 '23 at 23:42
  • In this case, most likely yes. Do they have an Android-specific SDK? – CommonsWare Jun 21 '23 at 10:46
  • I am now looking at using the IP2LOCATION DB3 data base, starting with the free edition to see if I can get it to work. I need to be able to load the DB3 into a data base in my app. The problem is that I have no experience with database programming so it is another big, technical learning experience. I am looking for an example that has a format similar to DB3 [several columns of data, retrieval only] so that I can get going on it. Have not found one yet. I'll keep digging. Thank you again :) – Mick Jun 22 '23 at 14:51

0 Answers0