[android]加密 URL 问题在 OKHttp

标签: json Android
发布时间: 2016/10/10 21:38:37
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这里的问题是我要在 URL API 传递参数,但问题是 json 在邮递员--> 身体工作--> 原料

通过传递参数︰

{"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然后不通过其他结果。 感谢帮助

解决方法 1:

 @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;
    }

它可以帮助你然后我会给你一个泛型类,为此,那就试着使用此代码。

赞助商