GET和POST链接

Post与Get一样 都是先创建一个按钮

<Button
        android:text="get请求"
        android:onClick="HttpGet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></Button>
    <Button
        android:text="post请求"
        android:onClick="HttPost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></Button>

提前要添加好网络权限

之后基本就是死代码了 靠手动记忆记住了

GET请求

public String getString(String path){

        try {
            URL url=new URL(path);
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(2000);
            httpURLConnection.setReadTimeout(2000);
            httpURLConnection.connect();
            if (httpURLConnection.getResponseCode()==200)
            {
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                String str="";
                StringBuilder stringBuilder=new StringBuilder();
                while ((str=bufferedReader.readLine())!=null)
                {
                    stringBuilder.append(str);
                }
                String json=stringBuilder.toString();
                Log.i("---json","getString"+json);
                return json;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
            return null;
    }

之后再前端调用即可

public void HttpGet(View view) {
        new MyAsyncTask().execute("http://43.143.146.165:7777/foods/getFoods?pageSize=10&currentPage=1");
    }
    public class MyAsyncTask extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... strings) {
            String path=strings[0];
            String json=HttpManager.getInstance().getString(path);
            return json;
        }
    }

Post的请求也相似


            Set<Map.Entry<String,Integer>> entries=hashMap.entrySet();
            for (Map.Entry<String,Integer> entry:entries)
            {
                String key=entry.getKey();
                Integer value=entry.getValue();
                if (isfirst)
                {
                    isfirst=false;
                }else
                {
                    boby+="&";
                }
                boby+= URLEncoder.encode(key,"UTF-8")+"="+value;

            }
            Log.i("---boby","postString"+boby);
            OutputStream outputStream=httpURLConnection.getOutputStream();
            outputStream.write(boby.getBytes());
            outputStream.flush();
            if (httpURLConnection.getResponseCode()==200)
            {
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                String str="";
                StringBuilder stringBuilder=new StringBuilder();
                while ((str=bufferedReader.readLine())!=null)
                {
                    stringBuilder.append(str);
                }
                String json=stringBuilder.toString();
                Log.i("---json","postString"+json);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

之后进行前端调用

public void HttPost(View view) {

new MuAsycTaskPoat().execute("http://43.143.146.165:7777/foods/postFoods");

}

public class MuAsycTaskPoat extends AsyncTask<String,Void,String>{

@Override

protected String doInBackground(String... strings) {

String path=strings[0];

HashMap<String,Integer> stringIntegerHashMap=new HashMap<>();

stringIntegerHashMap.put("currentPage",1);

stringIntegerHashMap.put("PageSize",10);

HttpManager.getInstance().postString(path,stringIntegerHashMap);

return null;

}

}

主要就是多敲 形成肌肉记忆 就行了 多敲多练

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值