【发布时间】:2012-04-06 20:34:16
【问题描述】:
我想在我的 Android 应用中使用 Google Products/Shopping API,但我对 HTTP GET 一无所知。我正在阅读this,它为我提供了所有这些不同的网址供我使用。那么如何通过 HTTP GET 在 Android 中使用 Google Products/Shopping API?
【问题讨论】:
标签: java android http http-get
我想在我的 Android 应用中使用 Google Products/Shopping API,但我对 HTTP GET 一无所知。我正在阅读this,它为我提供了所有这些不同的网址供我使用。那么如何通过 HTTP GET 在 Android 中使用 Google Products/Shopping API?
【问题讨论】:
标签: java android http http-get
先熟悉HTTP,然后再熟悉URLConnection和Apache HttpClient很有用。
【讨论】:
这是一些示例代码,我从服务器获取 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;
}
【讨论】: