2016-03-18 125 views
0

我需要一些“下载”网页内容...我的意思是,我需要有HTTP响应并将其存储到一个字符串变种。Android - 发送HTTP请求(API> 23)

例如,如果我插入www.example.com,响应将是这样的:<html><head></head><body>THIS IS AN EXAMPLE etc etc etc...<><><>

我尝试了一些代码我看见了,但他们是从API < 23,和Android已经删除HTTP APACHE。

预先感谢您

+0

你可能想考虑使用Retrofit。 – Michael

+0

Duplicate:http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests –

回答

1

HttpUrlConnection是apache库的替代品。

+0

@PaRiMaLRaJ是的。他说,v23中的Apache库已被删除,因此无法使用。他是对的。我告诉他什么API取代了他们。 –

+0

这正是我在找的东西......谢谢Gabe! – Parene

0

可以在gradle这个文件中添加

useLibrary 'org.apache.http.legacy' 

,你可以使用HTTP的Apache班API 23 +。

0

你有两种选择: 1-采用Android的URLConnection为EX:

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

2-以下内容添加到您的gradle这个文件

useLibrary 'org.apache.http.legacy' 
0

另一种方法是使用OKHttp库。

添加到您的gradle这个文件:

编译 'com.squareup.okhttp3:okhttp:3.2.0'

图书馆是使用比较简单:

OkHttpClient client = new OkHttpClient(); 
Request request = new Request.Builder() 
     .url(url) 
     .build(); 

Response response = client.newCall(request).execute(); 
String body = response.body().string();