So I'm trying to build a Tax/MOT checker where a user would enter a registration plate into my application, it sends the number plate into a car checker website (which scraps data from DVLA) and retrieves TAX and MOT information which is then sent back to the application once car details are viewed, I've researched a lot on the way as I just started learning more about android studio and complex java code in general. Anyway, the issue I seem to be having relates to when my html link is passed through HttpURLConnection where it instantly crashes the application. I looked in the logcat and it came up with android.os.NetworkOnMainThreadException as the main culprit.
I've seen other threads about this where an Async subclass can be used but that has since been deprecated in favour of java.util.concurrent which I have limited knowledge on.
Here is the code for my CarSearch (Tax/MOT check) function when the insert vehicle button is clicked, after the '2' toast I seem to run into the error:
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String vehiclenameTXT = name.getText().toString();
String numberplateTXT = plate.getText().toString();
String mileageTXT = mileage.getText().toString();
Toast.makeText(CarSearch.this, "1", Toast.LENGTH_SHORT).show();
Boolean checkinsertdata = DB.insertvehicledata(vehiclenameTXT, numberplateTXT, mileageTXT);
if(checkinsertdata==true)
{
Toast.makeText(CarSearch.this, "New Vehicle Added", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(CarSearch.this, "Cannot add the same vehicle details twice!", Toast.LENGTH_SHORT).show();
}
try {
Toast.makeText(CarSearch.this, "2", Toast.LENGTH_SHORT).show();
String addr = "https://cartaxcheck.co.uk/free-car-check/?vrm=LN11VUM";
URL url = new URL(addr);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
content.setText(inputLine);
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(CarSearch.this, "3", Toast.LENGTH_SHORT).show();
}
});
Here is the error I get thrown in the logcat:
2021-10-16 21:46:30.798 26220-26220/com.example.projectsupercars E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.projectsupercars, PID: 26220
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1605)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at com.android.okhttp.Dns$1.lookup(Dns.java:41)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
at com.example.projectsupercars.CarSearch$1.onClick(CarSearch.java:73)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I know what needs to be done but I have trouble seeing how to do it. How can I go about solving this issue?