什么是Http请求
Http请求是被客户端和服务器端之间,发送请求和返回应答的标准
什么是HttpUrlConnection
HttpURLConnection是一种多用途、轻量极的HTTP客户端,使用它来进行HTTP操作可以适用于大多数的应用程序。虽然HttpURLConnection的API提供的比较简单,但是同时这也使得我们可以更加容易地去使用和扩展它。
如何使用HttpUrlConnection
1.创建URL对象。
2.通过URL对象调用openConnection()方法获得HttpURLConnection对象。
3.HttpURLConnection对象设置其它连接属性。
4.HttpURLConnection对象调用getInputstream()方法下向服务器发送http请求并获取到服务器返回的输入流。
5读取输入流,转换成String字符串。
使用如何使用HttpUrlConnection获取Http请求
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
getWebInfo();
}
}).start();
}
});
}
private void getWebInfo() {
try {
//1.寻找水源--创建URL
URL url =new URL("https://www.youkuaiyun.com/");
//2.开水闸--openConnection
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
//3.建管道--InputStream
InputStream inputStream=httpURLConnection.getInputStream();
//4.建水池--InputStreamReader
InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"UTF-8");
//5.水桶盛水--BufferedReader
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
StringBuffer stringBuffer=new StringBuffer();
String temp=null;
while ((temp=bufferedReader.readLine())!=null){
stringBuffer.append(temp);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
Log.e("MAIN",stringBuffer.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void bindID() {
button=findViewById(R.id.getweb_btn);
}
HttpUrlConnection下载图片
MainActicity中的代码
private ProgressBar progressBar;
private Button button;
private ImageView imageView;
private String pic="http://p.yjbys.com/image/20170301/1488351756222990.png";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
bindID();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Down down =new Down(Main4Activity.this,imageView);
down.execute(pic,"pi1.jip");
}
});
}
private void bindID() {
imageView=findViewById(R.id.down_iv);
button=findViewById(R.id.down_btn);
}
down中的代码
public class Down extends AsyncTask<String,Integer,Integer> {
private String dirPATH;//下载图片的目录
private String filePATH;//文件具体的储存位置
private ImageView imageView;
private Context context;
public Down(Context context, ImageView imageView){
this.context=context;
this.imageView=imageView;
}
@Override
protected Integer doInBackground(String... strings) {
String fileName =strings[1];//要储存的图片名字
//判断目录是否存在,若不存在,创建目录
dirPATH= Environment.getExternalStorageDirectory()+"/pic/";
File dir=new File(dirPATH);//目录对象
if (!dir.exists()){
dir.mkdirs();
}
//判断文件是否存在,若不存在,创建文件
filePATH=dirPATH+fileName;
File file =new File(filePATH);//创建文件对象
if (file.exists()){
return -1;
}else {
try {
file.createNewFile();//不存在就创造出这个文件
} catch (IOException e) {
e.printStackTrace();
}
}
//创建输入流 输出流
InputStream inputStream=null;
OutputStream outputStream=null;
try {
URL url=new URL(strings[0]);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
if (httpURLConnection.getResponseCode()==200){
inputStream=httpURLConnection.getInputStream();
}else {
return -2;
}
outputStream=new FileOutputStream(file);
int length=0;
byte[] buffer=new byte[4*1024];//缓冲区
while ((length=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
switch (integer){
case 1:
//正常下载完成
Toast.makeText(context,"下载完成",Toast.LENGTH_SHORT).show();
Bitmap bitmap= BitmapFactory.decodeFile(filePATH);
imageView.setImageBitmap(bitmap);
break;
case -1:
//文件已存在
Toast.makeText(context,"文件已存在",Toast.LENGTH_SHORT).show();
break;
case -2:
//网络异常
Toast.makeText(context,"网络异常",Toast.LENGTH_SHORT).show();
break;
}
}
}