Reputation: 2470
I've got portion of code that gets data from the GPS and should save it back to the API.
try{
String Lat = URLEncoder.encode(Double.toString(loc.getLatitude()), "UTF-8");
String Lng = URLEncoder.encode(Double.toString(loc.getLongitude()), "UTF-8");
String currentLocationText = "My current location is: " + "Lat = " + Lat + "Lng = " + Lng;
Log.d(LOG_TAG, currentLocationText);
String urlString = "http://api/?lat="+Lat+"&lng="+Lng;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, "UTF-8");
String theString = writer.toString();
Log.d(LOG_TAG, "Server response: ."+ theString);
} catch (Exception e) {
Log.d(LOG_TAG, "Exception: "+ e.getMessage());
}
However during the runtime I receive in Logs:
"Exception: null"
In my manifest file I've put the:
<uses-permission android:name="android.permission.INTERNET" />
Sorry if it looks messy (exception handler), I am the PHP developer and that are my very first steps :).
How do I know what's the issue? Can I improve the Exception message to know what's going on?
EDIT: Got the stack trace
01-16 21:47:14.759: D/MyLocationListener(2041): Exception: null
01-16 21:47:14.784: D/MyLocationListener(2041): Stack trace: android.os.NetworkOnMainThreadException
01-16 21:47:14.784: D/MyLocationListener(2041): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
01-16 21:47:14.784: D/MyLocationListener(2041): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-16 21:47:14.784: D/MyLocationListener(2041): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-16 21:47:14.784: D/MyLocationListener(2041): at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
01-16 21:47:14.784: D/MyLocationListener(2041): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
01-16 21:47:14.784: D/MyLocationListener(2041): at com.example.gps_tracker.MyLocationListener.onLocationChanged(MyLocationListener.java:44)
01-16 21:47:14.784: D/MyLocationListener(2041): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
01-16 21:47:14.784: D/MyLocationListener(2041): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
01-16 21:47:14.784: D/MyLocationListener(2041): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
01-16 21:47:14.784: D/MyLocationListener(2041): at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 21:47:14.784: D/MyLocationListener(2041): at android.os.Looper.loop(Looper.java:176)
01-16 21:47:14.784: D/MyLocationListener(2041): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-16 21:47:14.784: D/MyLocationListener(2041): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 21:47:14.784: D/MyLocationListener(2041): at java.lang.reflect.Method.invoke(Method.java:525)
01-16 21:47:14.784: D/MyLocationListener(2041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-16 21:47:14.784: D/MyLocationListener(2041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-16 21:47:14.784: D/MyLocationListener(2041): at dalvik.system.NativeStart.main(Native Method)
I think I need to read that and fully understand that: How to fix android.os.NetworkOnMainThreadException?
Upvotes: 0
Views: 462
Reputation: 1052
You should do all of networking tasks using async task. Currently, the way you do is blocking the ui thread which is not permitted. That's why you're getting the error.
Check out this about how to use AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 1