HTTP俩种请求方式

文章展示了在Android环境中如何使用IntentService执行GET网络请求以及创建自定义线程进行POST网络请求的方法。通过HttpURLConnection实现连接设置、超时时间、数据读取等操作,并处理可能的异常情况。
摘要由CSDN通过智能技术生成

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);
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值