HTTP的两大种请求方式(Get和Post)

1.post请求进行上传方式
private GsonGet() {
}
private static GsonGet gsonGet=null;
public static GsonGet getInstance(){
    if (gsonGet==null){
        gsonGet=new GsonGet();
    }
    return  gsonGet;
}
public void post(String path){
    try {
        URL url = new URL(path);
        HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setConnectTimeout(10*1000);
        httpURLConnection.setReadTimeout(10*1000);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        String params="pageSize=10&currentPage=1";
        OutputStream outputStream=httpURLConnection.getOutputStream();
        outputStream.write(params.getBytes());
        outputStream.flush();
        if (httpURLConnection.getResponseCode()==200){
            InputStream inputStream=httpURLConnection.getInputStream();
            byte[]bytes =new byte[1024];
            int len=0;
            StringBuilder stringBuilder = new StringBuilder();
            while ((len=inputStream.read(bytes))!=-1){
                stringBuilder.append(new String(bytes,0,len));
            }
            String s = stringBuilder.toString();
            Log.i("--post", "run: "+s);
        }
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}
public void download(String url1){
    try {
        URL url = new URL(url1);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setConnectTimeout(10*1000);
        httpURLConnection.setReadTimeout(10*1000);
        if (httpURLConnection.getResponseCode()==200){
            InputStream inputStream=httpURLConnection.getInputStream();
            byte[]bytes=new byte[1024];
            int len=0;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                File path1=Environment.getExternalStorageDirectory();
                File file=new File(path1,"w.jpg");
                FileOutputStream fileOutputStream=new FileOutputStream(file);
                while ((len=inputStream.read())!=-1){
                    fileOutputStream.write(bytes,0,len);
                }
            }
        }
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public void upload(String url1,String file){
    String  BOUNDARY = UUID.randomUUID().toString();
    String PREFIX = "--" , LINE_END = "\r\n";
    String CONTENT_TYPE = "multipart/form-data";

    try {
        URL url = new URL(url1);
        HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setConnectTimeout(10*1000);
        httpURLConnection.setReadTimeout(10*1000);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestProperty("Content-Type",CONTENT_TYPE+";boundary="+BOUNDARY);
        httpURLConnection.connect();
        File file1=new File(file);
        if (file1!=null){
            OutputStream outputStream=httpURLConnection.getOutputStream();
            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append(PREFIX);
            stringBuffer.append(BOUNDARY);
            stringBuffer.append(LINE_END);
            stringBuffer.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file1.getName()+"\""+LINE_END);
            stringBuffer.append(LINE_END);
            outputStream.write(stringBuffer.toString().getBytes());
            InputStream inputStream=new FileInputStream(file1);
            byte[] bytes=new byte[1024];
            int len=0;
            while ((len=inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
            inputStream.close();
            outputStream.write(LINE_END.getBytes());
            byte[] end=(PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
            outputStream.write(LINE_END.getBytes());
            outputStream.write(end);
            outputStream.flush();
            if (httpURLConnection.getResponseCode()==200){

            }
        }
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

2.Get请求方式

public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            URL url = new URL("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=12");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(10*1000);
            httpURLConnection.setReadTimeout(10*1000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode()==200){
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                String str="";
                while ((str=bufferedReader.readLine())!=null){
                    stringBuilder.append(str);
                }
                String json=stringBuilder.toString();
                Log.i("fds", "onHandleIntent: "+json);
            }

        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值