Note that there are some explanatory texts on larger screens.

plurals
  1. POREST request on Android
    primarykey
    data
    text
    <p>I'm writing my first Android app where the first thing to do is collect a list of maps from a server, but I'm having difficulty getting a simple REST request working. I've been following a number of tutorials and am trying to employ both AsyncTask and HttpURLConnection to do this properly.</p> <p>For my simple initial test, I just call <code>getMapsJson</code>, with <code>serverUrl</code> set to "<a href="http://httpbin.org/ip" rel="nofollow">http://httpbin.org/ip</a>" (just to make sure it's sending and receiving data correctly).</p> <p><strong>Code</strong></p> <pre><code>public String getMapsJson() { Log.d(Globals.tag, "&gt;&gt; getting maps json from " + serverUrl.toString()); String output = ""; new GetMapsJsonTask().execute(serverUrl); return output; } private class GetMapsJsonTask extends AsyncTask&lt;URL, Void, String&gt; { private HttpURLConnection connection = null; protected String doInBackground(URL... url) { String output = ""; try { connection = (HttpURLConnection) url[0].openConnection(); connection.setRequestMethod("GET"); Log.d(Globals.tag, "connection opened!"); InputStream in = new BufferedInputStream(connection.getInputStream()); Log.d(Globals.tag, "input stream captured"); output = readStream(in); Log.d(Globals.tag, "stream read"); } catch (IOException e) { Log.d(Globals.tag, (e.getMessage() == null ? "IO Exception" : "IO : " + e.getMessage())); } finally { return output; } } protected void onPostExecute(String json) { Log.d(Globals.tag, "Json response: " + json); } } private String readStream(InputStream in) throws IOException { Log.d(Globals.tag, "entering readStream"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = reader.readLine(); String output = line; while((line = reader.readLine()) != null) output += line; return output; } </code></pre> <p><strong>AndroidManifest.xml</strong></p> <p>I have the android.permission.INTERNET line in there, but I'm not positive it's in the correct location. May cause issues?</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.northstar.minimap" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;application android:name="com.northstar.minimap.Globals" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.northstar.minimap.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name="com.northstar.minimap.MapActivity" android:label="@string/title_activity_map" android:parentActivityName="com.northstar.minimap.MainActivity" &gt; &lt;meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.northstar.minimap.MainActivity" /&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p><strong>Output</strong></p> <p>Logcat yields the following lines:</p> <pre><code>12-01 21:31:44.727 2188-2188/com.northstar.minimap D/Minimap﹕ &gt;&gt; getting maps json from http://httpbin.org/ip/ 12-01 21:31:44.727 2188-2232/com.northstar.minimap D/Minimap﹕ connection opened! 12-01 21:31:45.106 2188-2232/com.northstar.minimap D/Minimap﹕ IO: http://httpbin.org/ip/ 12-01 21:31:45.106 2188-2188/com.northstar.minimap D/Minimap﹕ Json response: </code></pre> <p>Basically, something's causing an IOException, and I'm not sure what - the exception's getMessage isn't exactly verbose. Do I have to do any configuration for the emulator (JellyBean, API level 17)?</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.
    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