使用Android进行HTTP请求

396

我已经到处搜寻,但找不到我的答案,有没有一种方法可以发起简单的HTTP请求? 我想要请求我网站上的一个PHP页面/脚本,但我不想显示网页。

如果可能,我甚至想在后台执行(在广播接收器中)。


这也与此相关:https://dev59.com/11YO5IYBdhLWcg3wN-6f - Mahozad
12个回答

505

更新

这是一个非常老的答案。我绝对不再推荐使用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");

11
以下是官方Android开发者博客关于AsyncTask的一篇文章链接:http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html - Austyn Mahoney
78
对于Android 2.3或更高版本,建议使用HttpURLConnection代替apache库进行HTTP请求。请参见http://android-developers.blogspot.com/2011/09/androids-http-clients.html。这样可以减少对电池的消耗并提高性能。 - Marty
8
responseString = out.toString() 需要放在 out.close() 调用之前。实际上,你应该将 out.close() 放在 finally 代码块中。总的来说,回答非常有帮助(+1),谢谢! - dcp
9
从蜂巢(SDK 11)开始,异步方式是最佳选择。当你尝试在主线程中运行HTTP请求时,会抛出一个NetworkOnMainThreadException异常。 - geaw35
2
这个答案非常优秀。但我建议不要使用AsyncTasks进行网络请求。它们很容易造成内存泄漏(实际上提供的示例确实有泄漏),并且不能提供人们对网络请求所期望的所有功能。考虑使用RoboSpice来处理这种后台任务:https://github.com/octo-online/robospice - Snicolas
显示剩余12条评论

68

14
为什么不推荐使用Apache HttpClient? - Ted
4
我的一个同谋在官方博客中详细阐述了这一点:http://android-developers.blogspot.com/2011/09/androids-http-clients.html - Elliott Hughes
我完全同意。毫无疑问,Apache httpclient提供了简单的方法和更抽象的协议视图,但Java的本机urlconnection并不少用。稍加动手,它与httpclient一样易于使用,并且更具可移植性。 - Nitin Bansal
1
实际上,如果您观看Google I/O 2010 - Android REST客户端应用程序(https://www.youtube.com/watch?v=xHXn3Kg2IQE 57min21sec)的视频,您会发现Apache HttpClient是最受推荐的。我引用Virgil Dobjanschi(谷歌Android应用程序组的软件工程师)的话:“我建议您使用HTTP Apache客户端,因为它具有更强大的实现。URL连接类型的HTTP事务不是最有效的实现方式。而且它终止连接的方式有时会对网络产生不利影响。” - Alan

50

注意:Android自带的Apache HTTP Client现已过时,建议使用HttpURLConnection。请参阅Android开发者博客以获取更多详细信息。

在您的清单中添加<uses-permission android:name="android.permission.INTERNET" />

然后您可以像这样检索网页:

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请求的更多信息。


1
@Semmix 怎么了?问题要求“一个简单的HTTP”请求,而我的代码恰好做到了这一点。 - Kevin Cronly
3
我理解你的第一个代码块是从Android文档复制粘贴的,但那个示例/文档真的很糟糕。readStream甚至没有定义。 - Eugene K
@EugeneK 他们是,但这可能是回答这个问题最简单的方法。在Android中正确地执行HTTP请求将涉及解释Retrofit和OkHttp。我认为这会比仅仅提供一个片段更让初学者困惑,即使它构造得不好,也可以从技术上实现简单的HTTP请求。 - Kevin Cronly
URL构造函数会抛出一个格式错误的异常,因此应该将其放在try catch块中进行包装。 - stebak

15

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

Volley提供以下好处:

自动调度网络请求。 多个并发网络连接。透明磁盘和内存响应缓存,并支持标准HTTP缓存一致性。支持请求优先级。取消请求API。您可以取消单个请求,也可以设置要取消的请求块或范围。易于自定义,例如,用于重试和退避。强大的排序使得从网络异步获取的数据正确地填充UI变得容易。调试和跟踪工具。

您可以像这样简单地发送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已经完成了所有这些工作。

12

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

implementation 'com.android.volley:volley:1.1.1'
将以下内容添加到AndroidManifest.xml中:
<uses-permission android:name="android.permission.INTERNET" />

并将以下内容添加到您的Activity代码中:

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客户端,而且非常简单。


但是这个请求是同步的,对吗?如何将其异步执行? - Dyno Cris
2
在我看来,它是异步的。在 queue.add(... 之后,您的代码将继续执行。 - Jarda Pavlíček
它是异步的。而我有相反的问题:如何进行阻塞式的HTTP(S)请求?除了在queue.add()之后组织一个繁忙循环之外,还有什么方法吗? - Violet Giraffe

6

由于没有任何答案描述如何使用OkHttp进行请求,而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();

这个库的明显优势在于它抽象了一些低层细节,提供了更友好和安全的交互方式。语法也被简化,可以编写出漂亮的代码。

6
private String getToServer(String service) throws IOException {
    HttpGet httpget = new HttpGet(service);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    return new DefaultHttpClient().execute(httpget, responseHandler);

}

问候


5

使用线程:

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

4

看看这个很棒的新库,可以通过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


4

我为一个使用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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接