Note that there are some explanatory texts on larger screens.

plurals
  1. PORESTful request using HTTPUrlConnection
    primarykey
    data
    text
    <p>I'm writing my first Android app, and trying to get the hang of making a REST call from within the app. Following a couple pages and other SO posts, I've got this:</p> <pre><code>URL serverUrl = new URL("http://localhost:13980/api/maps/"); //the value is hardcoded for testing purposes public String getMapsJson() { Log.d("Minimap", "&gt;&gt; getting maps json from " + serverUrl.toString()); String output = ""; HttpURLConnection connection = null; try { connection = (HttpURLConnection) serverUrl.openConnection(); connection.setRequestMethod("GET"); Log.d("Minimap", "connection opened!"); InputStream in = new BufferedInputStream(connection.getInputStream()); Log.d("Minimap", "input stream captured"); output = readStream(in); Log.d("Minimap", "stream read"); } catch (Exception e) { Log.d("Minimap", e.getMessage()); //************* This is line 53 &lt;&lt;&lt;&lt;&lt;&lt; } finally { connection.disconnect(); } Log.d("Minimap", "JSON: " + output); return output; } private String readStream(InputStream in) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = reader.readLine(); String output = line; while((line = reader.readLine()) != null) output += line; return output; } catch (IOException e) { e.printStackTrace(); return ""; } } </code></pre> <p>The output I'm getting from logcat is just</p> <pre><code>11-19 07:38:31.599 3813-3813/com.northstar.minimap D/Minimap﹕ &gt;&gt; getting maps json from http://localhost:13980/api/maps/ 11-19 07:38:31.599 3813-3813/com.northstar.minimap D/Minimap﹕ connection opened! </code></pre> <p>and then the app crashes with the following stack trace:</p> <pre><code>11-19 08:02:50.903 744-744/com.northstar.minimap E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.northstar.minimap/com.northstar.minimap.MapActivity}: java.lang.NullPointerException: println needs a message at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Native Method) at android.util.Log.d(Log.java:138) at com.northstar.minimap.Communicator.getMapsJson(Communicator.java:53) at com.northstar.minimap.MapActivity.onCreate(MapActivity.java:36) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)             at android.app.ActivityThread.access$600(ActivityThread.java:141)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)             at android.os.Handler.dispatchMessage(Handler.java:99)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method) 11-19 08:02:50.933 308-445/system_process W/ActivityManager﹕ Force finishing activity com.northstar.minimap/.MapActivity 11-19 08:02:50.943 308-445/system_process W/ActivityManager﹕ Force finishing activity com.northstar.minimap/.MainActivity </code></pre> <p>The page it's querying (<code>http://localhost:13980/api/maps/</code>) is a simple GET endpoint that spits back some JSON. At this point, I'd just be happy if the JSON it pulled was printed to LogCat.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload