在 OKHttp 中加密 URL 问题
Encrypt URL Issue in OKHttp
这里的问题是我必须在 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 然后传递结果,否则不要。
感谢帮助
@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;
}
尝试使用此代码,如果它对您有帮助,那么我会给您一个通用的 class。
尝试如下,
String postBodyParamsJson = "{\"from\":\"abc\",\"to\":\"pqr\",\"amount\":\"10000\"}";
RequestBody body = RequestBody.create(JSON, postBodyParamsJson);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
这里的问题是我必须在 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 然后传递结果,否则不要。 感谢帮助
@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;
}
尝试使用此代码,如果它对您有帮助,那么我会给您一个通用的 class。
尝试如下,
String postBodyParamsJson = "{\"from\":\"abc\",\"to\":\"pqr\",\"amount\":\"10000\"}";
RequestBody body = RequestBody.create(JSON, postBodyParamsJson);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();