在OKHttp中加密URL问题

时间:2016-09-20 07:14:36

标签: android json getjson okhttp okhttp3

这里的问题是我必须在URL API中传递参数,但问题是json在Postman中工作 - > Body - > raw

传递参数:

{"from":"abc","to":"pqr","amount":"10000"}

结果是:

{ 
"errFlag": "0",
"errMsg": "Success",
"result": "1745738346.397"
}

但是我们传递的内容 OkHttp 我们得到了另一个结果 这是我的代码Okhttp:

b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (isNetworkAvailable()) {
                RequestBody formBody = new FormBody.Builder()
                        .add("from", "abc")
                        .add("to", "pqr")
                        .add("amount", "10000")
                        .build();


                try {
                    post(Baseurl, formBody, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e("JSONDemo", "IOException", e);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String res = response.body().string();

                            Log.e("res", " " + res);


                            try {

                                JSONObject jsonObject=new JSONObject(res);
                                String errFlag=jsonObject.getString("errFlag");
                                if(errFlag.equals("0")){
                                    String a=jsonObject.getString("result");
                                    Log.e("hello........",a);
                                }else{
                                    Log.e("Error", "Wrong");
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        // you can access all the UI componenet


                                    }
                                });


                            } catch (Exception e) {
                                Log.e("JSONDemo", "onResponse", e);
                                e.printStackTrace();

                            }
                        }
                    });
                } catch (Exception e) {
                    Log.e("JSONDemo", "Post Exception", e);
                }


            }
        }
    });
 private final OkHttpClient client = new OkHttpClient();

Call post(String url, RequestBody formBody, Callback callback) throws IOException {

    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();

    Call call = client.newCall(request);
    call.enqueue(callback);
    return call;
}

输出是:

 {"errNum":"404","errFlag":"1","errMsg":"Some fields are missing"}

我只想要 errFlag = 0 然后传递结果。 感谢您的帮助

2 个答案:

答案 0 :(得分:2)

 @Override
    protected String doInBackground(String... params) {
        OkHttpClient client = new OkHttpClient();
        formBody.add("version", version);
        formBody.add("device_id", device_id);
        formBody.add("platform", "android");
        RequestBody body = formBody.build();
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();    
        try {
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) {
                result = response.toString();
            } else {

            }
            result = response.body().string();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

尝试使用此代码,如果它可以帮助您,那么我将为您提供一个通用类。

答案 1 :(得分:0)

尝试如下,

String postBodyParamsJson = "{\"from\":\"abc\",\"to\":\"pqr\",\"amount\":\"10000\"}";
RequestBody body = RequestBody.create(JSON, postBodyParamsJson);
Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();