在 onPostExecute 方法中使用 okhttp 响应 null 解析 json
parse json using okhttp response null in onPostExecute method
我正在为 OTP 开发演示。我收到短信,但问题是我没有在 onPostExecute 方法中收到回复。即使在 logcat 中显示响应,但我的 responseString 变量始终为空。
我的 responseString Toast 始终为空。请帮助我。
这是我的 AsyncTask
private class SendOTPTask extends AsyncTask<String, String, Void> {
String responseString;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
@Override
protected Void doInBackground(String... str) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Map<String, String> params = new HashMap<String, String>();
params.put("ContactNo", str[0]);
// params.put("name", "your name");
JSONObject parameter = new JSONObject(params);
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, parameter.toString());
Request request = new Request.Builder()
.url("http://202.131.126.46/StyleUpPlusApps.UserWebApi/api/OTP/SendOTPForRegistration")
.post(body)
.addHeader("content-type", "application/json; charset=utf-8")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
responseString = call.request().body().toString();
// Log.e("response error -->", call.request().body().toString());
new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage(responseString)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Log.e("response success -->", response.body().string());
responseString = response.body().string();
System.out.println(responseString);
/* if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
{
responseString = response.body().string();
System.out.println(responseString);
response.body().close();
}*/
}
});
return null;
}
@Override
protected void onPostExecute(Void s) {
super.onPostExecute(s);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(MainActivity.this, "" + responseString, Toast.LENGTH_SHORT).show();
/*if (!TextUtils.isEmpty(responseString)){
try {
JSONObject jsonObject = new JSONObject(responseString);
Toast.makeText(MainActivity.this, " "+responseString, Toast.LENGTH_SHORT).show();
if (jsonObject.getBoolean("IsSuccess")){
Toast.makeText(MainActivity.this, "OTP send...", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "something went wrong", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}*/
}
}
那是因为您从 doInBackground()
中 returning NULL。检查 doInBackground()
方法的最后一行并将其更改为 return responseString
这个
client.newCall(request).enqueue(new Callback() {
是一个 asynchronous
调用.. 在
一段时间后更新 responseString
public void onResponse(Call call, Response response)
所以发生的事情是:doInBackground
将调用此行并退出,onPostExecute
将在此之后立即调用,此时异步调用不会执行,因此 responseString 仍然是 null..明白了吗?
建议:由于你已经在使用asynctask,所以不要使用okhttp的异步调用,使用execute()代替enqueue(..)。
检查此以了解更多详细信息:
https://github.com/square/okhttp/wiki/Recipes
我正在为 OTP 开发演示。我收到短信,但问题是我没有在 onPostExecute 方法中收到回复。即使在 logcat 中显示响应,但我的 responseString 变量始终为空。
我的 responseString Toast 始终为空。请帮助我。
这是我的 AsyncTask
private class SendOTPTask extends AsyncTask<String, String, Void> {
String responseString;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
@Override
protected Void doInBackground(String... str) {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Map<String, String> params = new HashMap<String, String>();
params.put("ContactNo", str[0]);
// params.put("name", "your name");
JSONObject parameter = new JSONObject(params);
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, parameter.toString());
Request request = new Request.Builder()
.url("http://202.131.126.46/StyleUpPlusApps.UserWebApi/api/OTP/SendOTPForRegistration")
.post(body)
.addHeader("content-type", "application/json; charset=utf-8")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
responseString = call.request().body().toString();
// Log.e("response error -->", call.request().body().toString());
new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage(responseString)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Log.e("response success -->", response.body().string());
responseString = response.body().string();
System.out.println(responseString);
/* if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
{
responseString = response.body().string();
System.out.println(responseString);
response.body().close();
}*/
}
});
return null;
}
@Override
protected void onPostExecute(Void s) {
super.onPostExecute(s);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(MainActivity.this, "" + responseString, Toast.LENGTH_SHORT).show();
/*if (!TextUtils.isEmpty(responseString)){
try {
JSONObject jsonObject = new JSONObject(responseString);
Toast.makeText(MainActivity.this, " "+responseString, Toast.LENGTH_SHORT).show();
if (jsonObject.getBoolean("IsSuccess")){
Toast.makeText(MainActivity.this, "OTP send...", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "something went wrong", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}*/
}
}
那是因为您从 doInBackground()
中 returning NULL。检查 doInBackground()
方法的最后一行并将其更改为 return responseString
这个
client.newCall(request).enqueue(new Callback() {
是一个 asynchronous
调用.. 在
public void onResponse(Call call, Response response)
所以发生的事情是:doInBackground
将调用此行并退出,onPostExecute
将在此之后立即调用,此时异步调用不会执行,因此 responseString 仍然是 null..明白了吗?
建议:由于你已经在使用asynctask,所以不要使用okhttp的异步调用,使用execute()代替enqueue(..)。
检查此以了解更多详细信息: https://github.com/square/okhttp/wiki/Recipes