使用OKhttp发送数据到服务器

1,首先添加依赖

implementation 'com.squareup.okhttp3:okhttp:4.2.2'
    implementation 'com.google.code.gson:gson:2.8.6'

2,上传一个简单的字符串到服务器

String name = editText.getText.toString();

RequestBody requestBody = new FormBody.Builder()
                                .add("vendovID", name)
                                .build();

final OkHttpClient client = new OkHttpClient();
                        final Request request = new Request.Builder()
                                .url("http://192.168.123.195:5000/you/api")
                                .post(requestBody)
                                .build();
                        client.newCall(request).enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                final String res = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        textView.setText(res);
                                    }
                                });
                            }
                        });

3,上传一个json到服务器

首先定义一个实体类,get set方法

然后按照下面的方法

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
String name = editText.getText().toString();
                        final Entity entity = new Entity();
                        entity.setVendovID(MainActivity.this.name);
                        Gson gson = new Gson();
                        final String json = gson.toJson(entity);
                        RequestBody requestBody = RequestBody.create(
                                json, JSON);

final OkHttpClient client = new OkHttpClient();
                        final Request request = new Request.Builder()
                                .url("http://192.168.123.195:5000/you/api")
                                .post(requestBody)
                                .build();
                        client.newCall(request).enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                final String res = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        textView.setText(res);
                                    }
                                });
                            }
                        });

4,最后别忘了在Manifest添加权限,点击按钮发送后台可以接收到你当前发送的数据

菜鸟入门,写的不好希望各位大哥多多指教,共同进步

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值