0

when I implement the google map it worked fine, but when I add in the web scraping function it can build but app crashed when run, is the web scraping error? how can I modify it to able scrape the web table content and load into google map snippet? I would like to use the marker with snippet to show the web scraping content and the location together. Thanks.

public class mapActivity extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap map;
private final LatLng PERTH = new LatLng(-31.952854, 115.857342);
private final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
private final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);

private Marker markerPerth;
private Marker markerSydney;
private Marker markerBrisbane;


String text1 = null;
String text2 = null;
String text3 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    final
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    getdata();

}

private boolean getdata() {
    try {
        Document doc = Jsoup.connect("url").get();
        Elements data1 = doc.select("table");
        text1 = data1.select("tr").get(1).text();
        text2 = data1.select("tr").get(2).text();
        text3 = data1.select("tr").get(3).text();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}


@Override
public void onMapReady(GoogleMap map) {
    // Add some markers to the map, and add a data object to each marker.
    markerPerth = map.addMarker(new MarkerOptions()
            .position(PERTH)
            .title("Perth")
            .snippet("details"+ text1));


    markerSydney = map.addMarker(new MarkerOptions()
            .position(SYDNEY)
            .title("Sydney")
            .snippet("Details"+text2));


    markerBrisbane = map.addMarker(new MarkerOptions()
            .position(BRISBANE)
            .title("Brisbane")
            .snippet("Details"+text3));



}

below are the red error code:

2021-03-25 22:57:58.020 4987-4987/com.example.mpsd E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mpsd, PID: 4987
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mpsd/com.example.mpsd.mapActivity}: java.lang.IllegalArgumentException: Malformed URL: url
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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)
     Caused by: java.lang.IllegalArgumentException: Malformed URL: url
        at org.jsoup.helper.HttpConnection.url(HttpConnection.java:131)
        at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:70)
        at org.jsoup.Jsoup.connect(Jsoup.java:73)
        at com.example.mpsd.mapActivity.getdata(mapActivity.java:45)
        at com.example.mpsd.mapActivity.onCreate(mapActivity.java:39)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        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) 
     Caused by: java.net.MalformedURLException: no protocol: url
        at java.net.URL.<init>(URL.java:601)
        at java.net.URL.<init>(URL.java:498)
        at java.net.URL.<init>(URL.java:447)
        at org.jsoup.helper.HttpConnection.url(HttpConnection.java:129)
        at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:70) 
        at org.jsoup.Jsoup.connect(Jsoup.java:73) 
        at com.example.mpsd.mapActivity.getdata(mapActivity.java:45) 
        at com.example.mpsd.mapActivity.onCreate(mapActivity.java:39) 
        at android.app.Activity.performCreate(Activity.java:8000) 
        at android.app.Activity.performCreate(Activity.java:7984) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        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 have solved the problem, can refer to [Answer!](Adding markers from Google Maps in Asynctask)

  • What does the crash log say? You can have a look at [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) for getting and interpreting it. It would be good to add the relevant part to the question. – Markus Kauppinen Mar 25 '21 at 13:22
  • @MarkusKauppinen Hi, I have updated with the error code, Thanks. – kewino3217 Mar 25 '21 at 15:02
  • @MarkusKauppinen I believe is that web scraping problem, I have search some other web scraping answers and some of them are using AsyncTask, but I don't know how to combine it together.is there any tips to fix the problem? Thanks. – kewino3217 Mar 25 '21 at 15:08
  • Heh, you are actually trying to scrape the URL "url" as pointed above. You have `Document doc = Jsoup.connect("url").get();` Probably you meant `url` to be a variable tha contains the actual URL, so the method call would then be `Document doc = Jsoup.connect(url).get();` – Markus Kauppinen Mar 26 '21 at 08:23
  • @MarkusKauppinen the "url" in my task I written "www.???.com", due to privacy so I changed to "url". – kewino3217 Mar 29 '21 at 12:16
  • @TDG I am trying to understand those google result, but there situations is different from me. – kewino3217 Mar 29 '21 at 12:17
  • You should probably include the `"http://` or `"https://` in the URL as in [jsoup documentation](https://jsoup.org/cookbook/input/load-document-from-url). – Markus Kauppinen Mar 29 '21 at 12:21
  • @MarkusKauppinen I have solved the problem and a thousand thank for helping. – kewino3217 Mar 29 '21 at 16:48

0 Answers0