okHttps

首先导入jar包

然后写okhttputils

public class OkhttpUtils {

    /**
     * 通过okhttp的get请求方式获取网络数据
     * */
    public static String getJsonContent(String url) throws IOException {
//        1.导包  复制jar包---》黏贴到libs文件下---》选中jar包,鼠标右键---》选择Add As Library --》选择ok
//        2.创建客户端对象
        OkHttpClient client = new OkHttpClient();
//        3.设置发起请求的对象
        Request.Builder builder = new Request.Builder();
        Request request = builder.get().url(url).build();
//        4.客户端发起请求,获取响应对象
        Response response  = client.newCall(request).execute();
//        5.通过响应对象,判断请求是否成功
        if (response.isSuccessful()) {
//            6.获得响应体内容
            ResponseBody body = response.body();
//            7.获取对应的数据
            String msg = body.string();
            return  msg;
        }
        return null;
    }

    /**
     * 通过okhttp的post请求方式获取网络数据
     **/
    public static String postJsonContent(String path, Map<String,String>map){
//        1.导包
//        2.创建客户端对象
        OkHttpClient client = new OkHttpClient();
//        获取提交的请求参数
        FormBody.Builder builder = new FormBody.Builder();
        Set<String> keySet = map.keySet();
        for (String key:keySet){
            String value = map.get(key);
            builder.add(key,value);   //向请求体当中添加键值对的过程
        }
        RequestBody body = builder.build();
//        3.创建请求对象
        Request request = new Request.Builder().post(body).url(path).build();
//        4.发起请求,获得响应对象
        try {
            Response response = client.newCall(request).execute();
//            5.判断请求是否成功
            if (response.isSuccessful()) {
//                6.获得请求体内容
                ResponseBody responseBody = response.body();
                String msg = responseBody.string();
                return msg;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

然后在MainActivity的代码

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private String url = "http://218.244.149.129:9010/api/companylist.php?industryid=98";
    private String postUrl = "http://218.244.149.129:9010/api/companylist.php";
    private String body = "industryid=98";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        new MyAsycnTask().execute(url);
    }

    class MyAsycnTask extends AsyncTask<String,Void,String>{
        @Override
        protected String doInBackground(String... params) {
//            String content = ConnectionUtils.postJsonContent(params[0], body);
           /* Map<String,String>map = new HashMap<>();
            map.put("industryid","98");
            String content = null;
            try {
                content = ClientUtils.postJsonContent(params[0],map);
            } catch (IOException e) {
                e.printStackTrace();
            }*/
            String content = "";
            try {
                 content = OkhttpUtils.getJsonContent(params[0]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return content;
        }
        @Override
        protected void onPostExecute(String s) {
            if (s!=null&&!s.isEmpty()) {
                tv.setText(s);
            }
        }
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值