"/>

parse json using okhttp response null in onPostExecute method

I am developing demo for OTP. I am getting sms but problem is i am not getting response in onPostExecute method. Even response showing in logcat but my responseString variable always null.

my responseString Toast always null. please help me.

Here is my 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();
                }

            }*/


        }

    }
Read Full News
Submit your Tech Updates