Android上的REST请求

Android上的REST请求,android,android-asynctask,httpurlconnection,Android,Android Asynctask,Httpurlconnection,我正在编写我的第一个Android应用程序,首先要做的是从服务器收集地图列表,但我很难让一个简单的REST请求工作。我一直在学习一些教程,并尝试使用AsyncTask和HttpURLConnection来正确地完成这项工作 对于简单的初始测试,我只需调用getMapsJson,将serverUrl设置为“”(以确保它正确发送和接收数据) 代码 public String getMapsJson() { Log.d(Globals.tag, ">> getting maps j

我正在编写我的第一个Android应用程序,首先要做的是从服务器收集地图列表,但我很难让一个简单的REST请求工作。我一直在学习一些教程,并尝试使用AsyncTask和HttpURLConnection来正确地完成这项工作

对于简单的初始测试,我只需调用
getMapsJson
,将
serverUrl
设置为“”(以确保它正确发送和接收数据)

代码

public String getMapsJson()
{
    Log.d(Globals.tag, ">> getting maps json from " + serverUrl.toString());
    String output = "";

    new GetMapsJsonTask().execute(serverUrl);

    return output;
}

private class GetMapsJsonTask extends AsyncTask<URL, Void, String>
{
    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;
}
基本上,有什么东西导致了IOException,我不确定是什么-异常的getMessage并不完全详细。我是否必须对模拟器进行任何配置(JellyBean,API级别17)


谢谢

查看这篇关于阅读HttpUrlConnection inputStream的教程,你在真实的设备上试过了吗?你会得到同样的错误吗?@Selecsosi刚刚试过该链接中使用的行;同样的例外。另外,这里描述的编写方法看起来并不特别对REST友好(多个Android+REST教程指向了我目前使用的方法)。@Mike我还没有;我的Galaxy Nexus需要此API级别的尚未发布的更新。我将在几周内推出Nexus7(2013),但与此同时,我仍在使用模拟器。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.northstar.minimap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name="com.northstar.minimap.Globals"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.northstar.minimap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.northstar.minimap.MapActivity"
            android:label="@string/title_activity_map"
            android:parentActivityName="com.northstar.minimap.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.northstar.minimap.MainActivity" />
        </activity>
    </application>
</manifest>
12-01 21:31:44.727    2188-2188/com.northstar.minimap D/Minimap﹕ >> 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: