看起来 Apache 客户端已被弃用,有没有另一种方法可以在不安装花哨的库的情况下进行 http 请求?
我应该使用 Apache 客户端吗?如何导入,我的 Android Studio 不想导入 HttpClient、HttpGet、...
谢谢
你试过 HttpURlConnection 吗?
这是从服务器获取图像并将其显示到图像视图的示例代码:
private class SendHttpRequestTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL("http://xxx.xxx.xxx/image.jpg");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}catch (Exception e){
Log.e(TAG,e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
ImageView imageView = (ImageView) findViewById(ID OF YOUR IMAGE VIEW);
imageView.setImageBitmap(result);
}
}
我希望我能帮上忙
尝试这个。
URL url = new URL("your URL in String");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int httpResponse = conn.getResponseCode();