Android上的REST请求

时间:2013-12-01 21:37:11

标签: android android-asynctask httpurlconnection

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

对于我的初步测试,我只需拨打getMapsJsonserverUrl设置为“http://httpbin.org/ip”(只是为了确保正确发送和接收数据)。

代码

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;
}

的AndroidManifest.xml

我有android.permission.INTERNET行,但我不肯定它在正确的位置。可能会引起问题吗?

<?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>

输出

Logcat产生以下几行:

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:

基本上,某些东西导致IOException,我不知道是什么 - 异常的getMessage并不完全冗长。我是否必须为模拟器进行任何配置(JellyBean,API级别17)?

谢谢!

0 个答案:

没有答案