HTTP 两种请求方法

1、get 用 IntentService请求

public class MyIntentService extends IntentService {


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


    @Override
    protected void onHandleIntent(Intent intent) {

        HttpUtil.getInstance().get();



    }
public void get(){
        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){
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                StringBuilder stringBuilder = new StringBuilder();
                String str="";
                while ((str= bufferedReader.readLine())!=null){
                    stringBuilder.append(str);
                }
                Log.d("TAG", "zzz"+stringBuilder.toString());
            }

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

    }

2、用POST 线程请求

public class MyThread2 extends Thread{
    String name;

    public MyThread2(String name) {
        this.name = name;
        Log.i("MyThread2", "MyThread2");
    }

    @Override
    public void run() {
        super.run();
        HttpUtil.getInstance().post(name);

    }
}
 public void post(String name){
        try {
            URL url = new URL(name);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(10*1000);
            connection.setReadTimeout(10*1000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            String params="pageSize=10&currentPage=1";
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(params.getBytes());
            outputStream.flush();
            if (connection.getResponseCode()==200){
                InputStream inputStream = connection.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.d("--zcx","run:"+s);
            }

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

下载请求

 

 上传请求

 

public  void upload(String urlpath,String filepath){
       // 设置边界
        String  BOUNDARY = UUID.randomUUID().toString();
        //前面边界符合﹔结尾符号
        String PREFIX = "--" , LINE_END = "\r\n";
         // http类型
        String CONTENT_TYPE = "multipart/form-data";
        try {
            //连接
            URL url = new URL(urlpath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(10*1000);
            connection.setReadTimeout(10*1000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            //特殊要求
            connection.setRequestProperty("Content-Type", CONTENT_TYPE+";boundary="+BOUNDARY);
            connection.connect();
            //向外界服务器
            File file = new File(filepath);
            if (file!=null) {
                OutputStream outputStream = connection.getOutputStream();
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(PREFIX);
                stringBuffer.append(BOUNDARY);
                stringBuffer.append(LINE_END);
                stringBuffer.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file.getName()+"\""+LINE_END);
               stringBuffer.append(LINE_END);
                outputStream.write(stringBuffer.toString().getBytes());
                //开始写真实数据
                InputStream inputStream = new FileInputStream(file);
                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(end);
                outputStream.flush();
                if (connection.getResponseCode()==200){

                }

            }

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

HFS的设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值