我正试图在Android 6.0中找到如何做一些Http请求,
由于Android进入6.0版本,我无法找到任何帮助我改变6.0的内容。
我只是想在没有任何依赖的情况下对REST Api做一些HTTP请求
我已经做到了这一点,但它让我回归405而不是200。
@Override
protected Object doInBackground(Object[] params) {
public String rest_Url = "https://balblabla" ;
HttpURLConnection client = null;
try {
URL url = new URL(rest_Url);
client = (HttpURLConnection) url.openConnection();
if (client != null){
client.setRequestMethod("GET");
// client.setRequestProperty("Accept-Encoding", "identity");
client.setDoOutput(true);
}
OutputStream outputPost = new BufferedOutputStream(client.getOutputStream());
outputPost.flush();
if (outputPost!= null){outputPost.close();}
int responseCode = client.getResponseCode();
if (responseCode != 200){
String failure = "ECHEC";
System.out.println(failure);
}else {
String success = "BRAVO";
System.out.println(success);
}
} catch (IOException e) {e.printStackTrace();}
finally {
if(client != null){
client.disconnect();
}
}
return null;`
感谢您的回答。
答案 0 :(得分:1)
你必须添加第三方库。即使我遇到同样的问题,也没有其他选择。 Apache
答案 1 :(得分:1)
由于apd.http已从sdk 23弃用,你应该使用一些网络库。
您可以使用Apache Commons Android独立包。
或强>
有一些很棒的库可以帮助您在Android中发出HTTP请求。
您可以使用Android的Volley库
或者您可以使用OkHttp。
阅读this以便更好地理解。