0

可能重复:
如何在 android 中执行 Http GET?

我想在我的 Android 应用程序中使用 Google Products/Shopping API,但我对 HTTP GET 一无所知。我正在阅读这篇文章,它为我提供了所有这些不同的网址供我使用。那么如何通过 HTTP GET 在 Android 中使用 Google Products/Shopping API?

4

2 回答 2

1

首先熟悉HTTP,然后熟悉URLConnectionApache HttpClient很有用。

于 2012-04-06T20:37:22.680 回答
1

这是我从服务器获取 JSON 的一些示例代码。它包括通过 HTTP 连接到某些东西的基本代码行。

public JSONArray getQuestionsJSONFromUrl(String url, List<NameValuePair> params) {  
    // Making HTTP request
try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        String jsonData = reader.readLine();
        JSONArray jarr = new JSONArray(jsonData);
        is.close();
        return jarr;
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    return null;
}   
于 2012-04-06T20:39:17.707 回答