okhttp-3.9.1 通过post形式提交数据,服务器返回参数未赋值 5C
1、 OKhttp通过键值对,进行post请求正常,代码如下:
// post提交键值对

         String url = "http://11-appaccess.myzmodo.com/zmd/user/usrlogin";

        OkHttpClient client = new OkHttpClient();

        RequestBody requestBody = new FormBody.Builder()
                .add("username", "eddyli@zmodo.com")
                .add("password", "e10adc3949ba59abbe56e057f20f883e")
                .add("clienttype", "0")
                .add("language", "zh")
                .add("platform", "2")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String body = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("MainActivity", "body:" + body);
                    }
                });
            }
        });
2、**OKhttp通过json,进行post请求不正常**,代码如下:
//post提交json
        String url = "http://11-appaccess.myzmodo.com/zmd/user/usrlogin";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username", "eddyli@zmodo.com");
            jsonObject.put("password", "e10adc3949ba59abbe56e057f20f883e");
            jsonObject.put("clienttype", "0");
            jsonObject.put("language", "zh");
            jsonObject.put("platform", "2");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String json = jsonObject.toString();
        Log.i("MainActivity", "json:" + json);

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient client = new OkHttpClient();

        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String body = response.body().string();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("MainActivity", "body:" + body);
                    }
                });
            }
        });

3个回答

String jsonStr = "{\"username\":\"lisi\",\"nickname\":\"李四\"}";//json数据
可能是你的JSON数据格式有问题

Mike_Modern
mike_modern 我用这种格式试了,还是不行,提示参数未赋值
2 年多之前 回复

直接浏览器提交试试。然后对比你post的数据跟浏览器的。应该你有参数数据漏掉了

兄弟,你用这个绑定你要添加的参数,FormBody.Builder builder = new FormBody.Builder(); 例如username, builder.add("username", "eddyli@zmodo.com");添加完后, RequestBody requestBody = builder.build();,这样提交,能拿到数据

Mike_Modern
mike_modern 嗯,用键值对方式,是可以成功的。但是用json格式:application/json; charset=utf-8 ,没有成功
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐