首页 > 代码库 > android OkHttpUtils 使用JSON数据作为请求参数

android OkHttpUtils 使用JSON数据作为请求参数

如果就直接将JSON作为请求字符串,服务端会出现中文乱码。所以只需要将请求的整个JSON参数字符串编码一次,然后服务端解码一次。我这里服务端使用的servlet,下面会两段代码贴出。

android:

                    String url = customSessionSingleton.getInstance().requestBaseUrl+"AlipayInterface";
                    try {
                        OkHttpUtils.post().url(url).addParams("masterParameter", URLEncoder.encode(passSessionData.toString(), "UTF-8")) .build().execute(new StringCallback() {
                            @Override
                            public void onBefore(Request request, int id) {

                            }
                            @Override
                            public void onAfter(int id) {

                            }
                            @Override
                            public void one rror(Call call, Exception e, int id) {
                                e.printStackTrace();
                                String json=  e.getMessage();
                            }
                            @Override
                            public void onResponse(String response, int id) {
                                JSONObject    returnJSONObject = null;
                                try {
                                    returnJSONObject=new JSONObject(response) ;

                                    if(returnJSONObject.getInt( "Success" )==1)
                                    {
                                        if(payWay.equals( "支付宝" )) {


                            serverSignStr=serverSignStr+"&sign=" + returnJSONObject.getString( "sign" );

                                        Runnable payRunnable = new Runnable() {

                                            @Override
                                            public void run() {
                                                PayTask alipay = new PayTask(HomeFragment_RechargeActivity.this);
                                                Map<String, String> result = alipay.payV2(serverSignStr, true);
                                                Log.i("msp", result.toString());

                                                Message msg = new Message();
                                                msg.what = SDK_PAY_FLAG;
                                                msg.obj = result;
                                                alipayHandle.sendMessage(msg);
                                            }
                                        };

                                        Thread payThread = new Thread(payRunnable);
                                        payThread.start();

                                        }
                                        else if(payWay.equals( "微信" ))
                                        {


                                        }
                                        else if(payWay.equals( "全积分抵扣" ))
                                        {

                                         Dialog alertDialog = new AlertDialog.Builder(HomeFragment_RechargeActivity.this).
                                    setTitle("登录用户,全积分抵扣").
                                    setMessage("全积分抵扣").
                                    setIcon(R.drawable.ic_launcher).
                                    create();
                            alertDialog.show();

                                                mLoading.dismiss();


                                        }
                                    }
                                    else
                                    {

                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }






                            }
                        });
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

 

Servlet:

    JSONObject    requestObject=new JSONObject(URLDecoder.decode(request.getParameter("masterParameter"),"utf-8"));
               String account= requestObject.getString("account");

 

android OkHttpUtils 使用JSON数据作为请求参数