【问题标题】:Make an HTTP request with android使用 Android 发出 HTTP 请求
【发布时间】:2023-02-06 00:49:30
【问题描述】:

我到处搜索但找不到答案,有没有办法发出简单的 HTTP 请求?我想在我的网站之一上请求一个 PHP 页面/脚本,但我不想显示该网页。

如果可能的话,我什至想在后台进行(在 BroadcastReceiver 中)

【问题讨论】:

标签: android httpwebrequest androidhttpclient


【解决方案1】:

更新

这是一个非常古老的答案。我绝对不会再推荐 Apache 的客户端了。而是使用:

原始答案

首先,请求访问网络的权限,将以下内容添加到您的清单中:

<uses-permission android:name="android.permission.INTERNET" />

那么最简单的方法就是使用与 Android 捆绑的 Apache http 客户端:

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet(URL));
    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        String responseString = out.toString();
        out.close();
        //..more logic
    } else{
        //Closes the connection.
        response.getEntity().getContent().close();
        throw new IOException(statusLine.getReasonPhrase());
    }

如果你想让它在单独的线程上运行,我建议扩展 AsyncTask:

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                responseString = out.toString();
                out.close();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }
    
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

然后您可以通过以下方式提出请求:

   new RequestTask().execute("http://stackoverflow.com");

【讨论】:

  • 这是一篇来自官方 android 开发者博客关于 AsyncTask 的文章:android-developers.blogspot.com/2010/07/…
  • 对于姜饼或更大的它实际上建议使用 HttpURLConnection 而不是 apache 库,请参阅android-developers.blogspot.com/2011/09/…。它对电池的负担更小,性能更好
  • responseString = out.toString() 需要在 out.close() 调用之前。实际上,您可能应该将 out.close() 放在 finally 块中。但总的来说,非常有帮助的答案(+1),谢谢!
  • 从 Honeycomb (SDK 11) 开始,异步方法是必经之路。当您尝试从主线程运行 HTTP 请求时,会抛出 NetworkOnMainThreadException
  • 这个答案相当优秀。但我建议不要将 AsyncTasks 用于网络。它们可以很容易地造成内存泄漏(实际上提供的示例确实会泄漏),并且不提供网络请求所期望的所有功能。考虑将 RoboSpice 用于此类后台任务:github.com/octo-online/robospice
【解决方案2】:

除非您有明确的理由选择 Apache HttpClient,否则您应该更喜欢 java.net.URLConnection。您可以在网络上找到大量有关如何使用它的示例。

自您的原始帖子:http://developer.android.com/reference/java/net/HttpURLConnection.html 以来,我们还改进了 Android 文档

我们已经在官方博客上讨论了权衡:http://android-developers.blogspot.com/2011/09/androids-http-clients.html

【讨论】:

  • 为什么不推荐使用 Apache HttpClient?
  • 我的一个同谋在官方博客上对此进行了非常详细的介绍:android-developers.blogspot.com/2011/09/…
  • @ElliottHughes:我 100% 同意。不可否认,Apache httpclient 提供了简单的方法和更抽象的协议视图,但 java 的本机 urlconnection 的用处丝毫不减。稍加动手,它就像 httpclient 一样易于使用,而且更便携
  • 实际上,如果您看一下视频 Google I/O 2010 - Android REST 客户端应用程序(youtube.com/watch?v=xHXn3Kg2IQE 57min21sec),您会发现 Apache HttpClient 是最受推荐的一个。我引用 Virgil Dobjanschi(一位在 Android 应用程序组工作的谷歌软件工程师)“我只是建议您使用 HTTP Apache 客户端,因为它具有更强大的实现。HTTP 事务的 URL 连接类型不是最有效的实现。它终止连接的方式有时会对网络产生不利影响。”
【解决方案3】:

注意:与 Android 捆绑在一起的 Apache HTTP 客户端现已弃用,取而代之的是 HttpURLConnection。请查看 Android Developers Blog 了解更多详情。

&lt;uses-permission android:name="android.permission.INTERNET" /&gt; 添加到您的清单中。

然后你会像这样检索一个网页:

URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
}
finally {
     urlConnection.disconnect();
}

我还建议在单独的线程上运行它:

class RequestTask extends AsyncTask<String, String, String>{

@Override
protected String doInBackground(String... uri) {
    String responseString = null;
    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
            // Do normal input or output stream reading
        }
        else {
            response = "FAILED"; // See documentation for more info on response handling
        }
    } catch (ClientProtocolException e) {
        //TODO Handle problems..
    } catch (IOException e) {
        //TODO Handle problems..
    }
    return responseString;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    //Do anything with response..
}
}

有关响应处理和 POST 请求的更多信息,请参阅 documentation

【讨论】:

  • @Semmix 怎么会这样?该问题要求“一个简单的 HTTP”请求,而我的代码正是这样做的。
  • 我知道您的第一个代码块是从 Android 文档复制粘贴的,但是 man 是示例/文档垃圾。 readStream 甚至没有定义。
  • @EugeneK 他们是,但这可能是回答这个问题的最简单方法。在 Android 中正确执行 HTTP 请求需要解释 Retrofit 和 OkHttp。我认为这会让初学者感到困惑,而不仅仅是分发一个技术上会发出简单 HTTP 请求的 sn-p,即使它的构造很差。
  • URL 构造函数抛出格式错误的异常,应该包含在 try catch 块中。
【解决方案4】:

最简单的方法是使用名为Volley 的 Android 库

排球提供以下好处:

自动调度网络请求。多个并发网络 连接.透明磁盘和内存响应缓存 标准 HTTP 缓存一致性。支持请求优先级排序。 取消请求 API。您可以取消单个请求,也可以 设置要取消的请求的块或范围。易于定制,对于 例如,重试和退避。强大的排序使其易于 使用从 网络。调试和跟踪工具。

您可以像这样简单地发送一个 http/https 请求:

        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://www.yourapi.com";
        JsonObjectRequest request = new JsonObjectRequest(url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if (null != response) {
                         try {
                             //handle your response
                         } catch (JSONException e) {
                             e.printStackTrace();
                         }
                    }
                }
            }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

在这种情况下,您不必考虑自己“在后台运行”或“使用缓存”,因为所有这些都已由 Volley 完成。

【讨论】:

    【解决方案5】:

    按照上面的建议使用 Volley。将以下内容添加到 build.gradle (Module: app)

    implementation 'com.android.volley:volley:1.1.1'
    

    将以下内容添加到 AndroidManifest.xml 中:

    <uses-permission android:name="android.permission.INTERNET" />
    

    并向您添加以下活动代码:

    public void httpCall(String url) {
    
        RequestQueue queue = Volley.newRequestQueue(this);
    
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // enjoy your response
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // enjoy your error status
                    }
        });
    
        queue.add(stringRequest);
    }
    

    它取代了http客户端,非常简单。

    【讨论】:

    • 但是这个请求是同步的,是吗?如何异步执行?
    • 恕我直言,它是异步的。您的代码在queue.add(... 之后继续
    • 它是异步的。我有相反的问题:如何发出阻塞 HTTP(S) 请求?也就是说,在queue.add() 之后没有组织一个繁忙的循环。
    【解决方案6】:
    private String getToServer(String service) throws IOException {
        HttpGet httpget = new HttpGet(service);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        return new DefaultHttpClient().execute(httpget, responseHandler);
    
    }
    

    问候

    【讨论】:

      【解决方案7】:

      用一个线程:

      private class LoadingThread extends Thread {
          Handler handler;
      
          LoadingThread(Handler h) {
              handler = h;
          }
          @Override
          public void run() {
              Message m = handler.obtainMessage();
              try {
                  BufferedReader in = 
                      new BufferedReader(new InputStreamReader(url.openStream()));
                  String page = "";
                  String inLine;
      
                  while ((inLine = in.readLine()) != null) {
                      page += inLine;
                  }
      
                  in.close();
                  Bundle b = new Bundle();
                  b.putString("result", page);
                  m.setData(b);
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
              handler.sendMessage(m);
          }
      }
      

      【讨论】:

        【解决方案8】:

        由于没有一个答案描述了使用 OkHttp 执行请求的方法,这是当今非常流行的 Android 和 Java HTTP 客户端,我将提供一个简单的示例:

        //get an instance of the client
        OkHttpClient client = new OkHttpClient();
        
        //add parameters
        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://www.example.com").newBuilder();
        urlBuilder.addQueryParameter("query", "stack-overflow");
        
        
        String url = urlBuilder.build().toString();
        
        //build the request
        Request request = new Request.Builder().url(url).build();
        
        //execute
        Response response = client.newCall(request).execute();
        

        这个库的明显优势是它从一些底层细节中抽象出来,提供更友好和安全的方式来与它们交互。语法也得到了简化,可以编写出漂亮的代码。

        【讨论】:

          【解决方案9】:

          我使用 Gson 库为 URL 请求的 Web 服务做了这个:

          客户:

          public EstabelecimentoList getListaEstabelecimentoPorPromocao(){
          
                  EstabelecimentoList estabelecimentoList  = new EstabelecimentoList();
                  try{
                      URL url = new URL("http://" +  Conexao.getSERVIDOR()+ "/cardapio.online/rest/recursos/busca_estabelecimento_promocao_android");
                      HttpURLConnection con = (HttpURLConnection) url.openConnection();
          
                      if (con.getResponseCode() != 200) {
                              throw new RuntimeException("HTTP error code : "+ con.getResponseCode());
                      }
          
                      BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
                      estabelecimentoList = new Gson().fromJson(br, EstabelecimentoList.class);
                      con.disconnect();
          
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  return estabelecimentoList;
          }
          

          【讨论】:

            【解决方案10】:

            看看这个很棒的新库,它可以通过 gradle 获得:)

            build.gradle:compile 'com.apptakk.http_request:http-request:0.1.2'

            用法:

            new HttpRequestTask(
                new HttpRequest("http://httpbin.org/post", HttpRequest.POST, "{ "some": "data" }"),
                new HttpRequest.Handler() {
                  @Override
                  public void response(HttpResponse response) {
                    if (response.code == 200) {
                      Log.d(this.getClass().toString(), "Request successful!");
                    } else {
                      Log.e(this.getClass().toString(), "Request unsuccessful: " + response);
                    }
                  }
                }).execute();
            

            https://github.com/erf/http-request

            【讨论】:

              【解决方案11】:

              这是 android 中 HTTP Get/POST 请求的新代码。 HTTPClient 已弃用,可能无法像我的情况那样使用。

              首先在build.gradle中添加两个依赖:

              compile 'org.apache.httpcomponents:httpcore:4.4.1'
              compile 'org.apache.httpcomponents:httpclient:4.5'
              

              然后把这段代码写在ASyncTaskdoBackground方法中。

               URL url = new URL("http://localhost:8080/web/get?key=value");
               HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
               urlConnection.setRequestMethod("GET");
               int statusCode = urlConnection.getResponseCode();
               if (statusCode ==  200) {
                    InputStream it = new BufferedInputStream(urlConnection.getInputStream());
                    InputStreamReader read = new InputStreamReader(it);
                    BufferedReader buff = new BufferedReader(read);
                    StringBuilder dta = new StringBuilder();
                    String chunks ;
                    while((chunks = buff.readLine()) != null)
                    {
                       dta.append(chunks);
                    }
               }
               else
               {
                   //Handle else
               }
              

              【讨论】:

              • 代码可能会被弃用,Android 平台 API 28 不再支持 apache。在这种情况下,您可以在清单或模块级 Gradle 文件中启用 apache 遗留属性。但是,建议使用 OKHttp、Volley 或 Retrofit 网络库。
              【解决方案12】:

              对我来说,最简单的方法是使用名为Retrofit2 的库

              我们只需要创建一个包含我们的请求方法、参数的接口,我们还可以为每个请求制作自定义标头:

                  public interface MyService {
              
                    @GET("users/{user}/repos")
                    Call<List<Repo>> listRepos(@Path("user") String user);
              
                    @GET("user")
                    Call<UserDetails> getUserDetails(@Header("Authorization") String   credentials);
              
                    @POST("users/new")
                    Call<User> createUser(@Body User user);
              
                    @FormUrlEncoded
                    @POST("user/edit")
                    Call<User> updateUser(@Field("first_name") String first, 
                                          @Field("last_name") String last);
              
                    @Multipart
                    @PUT("user/photo")
                    Call<User> updateUser(@Part("photo") RequestBody photo, 
                                          @Part("description") RequestBody description);
              
                    @Headers({
                      "Accept: application/vnd.github.v3.full+json",
                      "User-Agent: Retrofit-Sample-App"
                    })
                    @GET("users/{username}")
                    Call<User> getUser(@Path("username") String username);    
              
                  }
              

              最好的是,我们可以使用 enqueue 方法轻松地异步完成

              【讨论】:

                猜你喜欢
                • 2011-03-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-10-20
                • 2021-07-20
                相关资源
                最近更新 更多