访问网络资源
程序员文章站
2022-05-05 20:39:33
...
URL访问网络图片
private void initURLLoadImage() {
final ImageView showImage = findViewById(R.id.showImage);
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("https://img12.360buyimg.com/babel/s590x470_jfs/t1/95464/30/9251/74407/5e0d6139E878bf1f3/09c7a2b5d96aec27.jpg.webp");
InputStream in = url.openStream();;
final Bitmap bitmap = BitmapFactory.decodeStream(in);
runOnUiThread(new Runnable() {
@Override
public void run() {
showImage.setImageBitmap(bitmap);
}
});
in.close();
/*写入文件中*/
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + System.currentTimeMillis()+".png";
File file=new File(path);
if (!file.exists()){
file.createNewFile();
}
InputStream i = url.openStream();
FileOutputStream fileOutputStream=new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buff = new byte[1024];
int len ;
while ((len = i.read(buff))!= -1){
bufferedOutputStream.write(buff,0,len);
}
bufferedOutputStream.close();
fileOutputStream.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
URLConnection请求提交
1、get请求
/**
* @param url
* @param params
*/
public void GET_Request(String url,String params){
String getUrl = url+"?"+params;//params的格式(val=key&val=key)
try {
URL url1 = new URL(getUrl);
URLConnection conn = url1.openConnection();
/*配置通用属性 */
conn.addRequestProperty("accept","*/*");
conn.addRequestProperty("","");
conn.addRequestProperty("","");
/*建立实际连接请求*/
conn.connect();
/*获取所有请求响应头*/
Map<String, List<String>> map = conn.getHeaderFields();
/*响应的数据*/
InputStream in = conn.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
String result = null;
String line ;
while ((line = bufferedReader.readLine())!=null){
result += line ;
}
Log.v("========","result="+result);
bufferedReader.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2、post请求
/**
* @param url
* @param params
*/
public void POST_Request(String url,String params){
String getUrl = url+"?"+params;//params的格式(val=key&val=key)
try {
URL url1 = new URL(getUrl);
URLConnection conn = url1.openConnection();
/*配置通用属性 */
conn.addRequestProperty("","");
conn.addRequestProperty("","");
conn.addRequestProperty("","");
/*post必须添加这两行 而且顺序不可变*/
conn.setDoOutput(true);
conn.setDoInput(true);
PrintWriter bufferedWriter=new PrintWriter (conn.getOutputStream());
/*发送请求*/
bufferedWriter.write(params);
bufferedWriter.print(params);
/*缓冲*/
bufferedWriter.flush();
/*获取所有请求响应头*/
Map<String, List<String>> map = conn.getHeaderFields();
/*响应的数据*/
InputStream in = conn.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
String result = null;
String line ;
while ((line = bufferedReader.readLine())!=null){
result += line ;
}
Log.v("========","result="+result);
bufferedReader.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
使用Http访问网络
HttpURLConnection继承了URLConnection,因此也可实现网络get 、post等请求。
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DownThread extends Thread {
private String url;
private File file;
public DownThread(String url,File file){
this.url = url ;
this.file = file;
}
@Override
public void run() {
super.run();
try {
URL urlReq = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlReq.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10*1000);
/**/
connection.addRequestProperty("Accept","");
connection.addRequestProperty("charset","utf-8");
/*获取响应io流*/
InputStream in = connection.getInputStream();
FileOutputStream fileOutputStream=new FileOutputStream(file);
BufferedOutputStream outputStream=new BufferedOutputStream(fileOutputStream);
int len = 0;
int line ;
byte[] buff = new byte[1024];
while ((line = in.read(buff))!=-1){
/*下载文件的大小*/
len += line ;
/*写入文件*/
outputStream.write(buff,0,len);
}
in.close();
fileOutputStream.close();
outputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下一篇: 显示和隐藏一个div的问题