0

i make a application for track geo location and save that data to server mysql database. when i run the application is it show unexpected error and close application. some times geolocation is not pointing. but when i comment that save code geo location tracking working nicely. cant find what is the exact problem in this here my code

///////////////// this is the updated locations/////////////

public void onLocationChanged(Location location) {

    String msg = "Updated Location: " +
            Double.toString(location.getLatitude()) + "," +
            Double.toString(location.getLongitude());


    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    loadSave();

    // Report to the UI that the location was updated

}

public void loadSave() {
            String mydataurl = "http://myrul.com/saveData.php";
        //String type = params[0];
    try {

        String longi = "aaa"; // Double.toString(location.getLongitude());
        String lati = "aare";//Double.toString(location.getLatitude());
        String user = "a";
        String code = "b";
        String date = "c";
        URL url = new URL(mydataurl);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("longi", "UTF-8") + "=" + URLEncoder.encode(longi, "UTF-8") + "&" +
                URLEncoder.encode("lati", "UTF-8") + "=" + URLEncoder.encode(lati, "UTF-8") + "&" +
                URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") + "&" +
                URLEncoder.encode("code", "UTF-8") + "=" + URLEncoder.encode(code, "UTF-8") + "&" +
                URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date, "UTF-8");
        bufferedWriter.write(mydataurl);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
                } catch (MalformedInputException e) {
        e.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

STACK TRACE

enter image description here

Chara
  • 31
  • 10
  • Could you add a stacktrace? – Jesse Buss Apr 25 '16 at 03:22
  • Stack-traceFATAL EXCEPTION: main Process: com.example.mapdemo, PID: 847 android.os.NetworkOnMainThreadException android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156) at java.net.InetAddress.lookupHostByName(InetAddress.java:418) at java.net.InetAddress.getAllByNameImpl – Chara Apr 25 '16 at 03:26
  • You can't make network calls on the main thread. Look into AsyncTasks. – Jesse Buss Apr 25 '16 at 03:31
  • can u little explain about this? means i have to make this loadSave() in another class or what i exactly want to do to fix this problem – Chara Apr 25 '16 at 03:34
  • You need to do the network request in a separate thread. If you move the network request inside of loadSave() into an AsyncTask, things should work for you! See examples here: http://developer.android.com/reference/android/os/AsyncTask.html – Jesse Buss Apr 25 '16 at 03:36
  • Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – OneCricketeer Apr 25 '16 at 03:43
  • Please copy the text of your exception next time – OneCricketeer Apr 25 '16 at 03:43
  • thanks can u publish correct code. because i new to android – Chara Apr 25 '16 at 03:48

1 Answers1

0

Android thought this exception because you executed network task on main thread. You have execute this task on other thread by put it to an AsyncTask.

 new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(final Void... params) {
            loadSave();
            return null;
        }
    }.execute();
Brian Hoang
  • 1,111
  • 9
  • 18
  • thanks can u publish full code set where i want to add this.. because i new to android – Chara Apr 25 '16 at 03:42
  • Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); loadSave(); – Brian Hoang Apr 25 '16 at 03:50
  • i change code like you said but there is an error cannot find symbol class AsyncTask E:\android-google-maps-demo-master\app\src\main\java\com\example\mapdemo\MapDemoActivity.java Error:(212, 13) error: cannot find symbol class AsyncTask c Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. Information:BUILD FAILED – Chara Apr 25 '16 at 04:01
  • Have you imported AsyncTask to your class – Brian Hoang Apr 25 '16 at 04:03
  • thanks friend.. now it working without errors. but data is not save to DB.. why is that ? – Chara Apr 25 '16 at 04:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110093/discussion-between-chara-and-hoang-v-nam). – Chara Apr 25 '16 at 04:24
  • here my php code require "DB_Connection.php"; $longi =$_POST["longi"]; $lati = $_POST["lati"]; $user = $_POST["user"]; $lisence =$_POST["lisence"]; $date = $_POST["date"]; $query1="INSERT INTO mapdata (longi,lati,user,lisence,date) values ('".$longi."','".$lati."','".$user."','".$lisence."', '".$date."' )"; mysql_query($query1) or die ('Error :' . mysql_error()); – Chara Apr 25 '16 at 04:39