【问题标题】:How to use HTTP GET with Android? [duplicate]如何在 Android 中使用 HTTP GET? [复制]
【发布时间】:2012-04-06 20:34:16
【问题描述】:

可能重复:
how do perform Http GET in android?

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

【问题讨论】:

    标签: java android http http-get


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这是一些示例代码,我从服务器获取 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-12-05
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 2013-06-15
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多