0

This is my Doinbackground,and for some reason i don't find anything in String s of postexecute,am i doing anything wrong ??,btw calling this asynctask by new mytask().execute(Url,dum) from another class

@Override
        protected String doInBackground(String... strings) {
            String email = strings[0];

            Request.Builder builder = new Request.Builder();
            // String json1="{'dashboardData':'SERVICE_GET_CCAVENUE_KEY','userName':'abcd'}";

            RequestBody formBody = new FormBody.Builder()
                    .add("REQUEST_TYPE_SENT","SERVICE_FORGOT_USER_PASSWORD")
                    .add("phone",strings[1])//"+91 - 9999991212")
                    .add("device_token","gf")
                    .add("device_type","2")
                    .build();


            builder.url(strings[1])
                    .post(formBody)
                    .build();



            Request request = new Request.Builder()
                    .url(strings[1])
                    .addHeader("decode","2")
                    .post(formBody)
                    .build();

            try {

                Response response = client.newCall(request).execute();

               // JSONObject obj = new JSONObject(response.body().string());
                //op[0] = obj.getString("user_id");
                writeToFile("response="+response.body().string());
                return response.body().string();

            }catch (Exception e){
                e.printStackTrace();
            }



            return null;
        }

 @Override
        protected void onPostExecute(String s) {

            //Context context = getApplicationContext();
            //CharSequence text = s;
            //int duration = Toast.LENGTH_SHORT;

            //Toast toast = Toast.makeText(context, text, duration);
            //toast.show();
           // Intent intent = new Intent(getBaseContext(), HomepageNormal.class);
           // startActivity(intent);
        }
Venky
  • 1,929
  • 3
  • 21
  • 36

4 Answers4

1

Instead of this

writeToFile("response="+response.body().string());
return response.body().string();

try using this

String tmpVariable = response.body().string();
writeToFile("response="+tmpVariable );
return tmpVariable ;
0

You must ref this page how to use async task first...

Community
  • 1
  • 1
Vijay
  • 227
  • 3
  • 18
0
 private class MyTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

          // Do stuff
          return result;//This result will  be passed to your OnPostExecute
    }

    @Override
    protected void onPostExecute(String result) {

              /*result is the result from doInBackground
               use this result to do stuffs*/

    } 

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}
  • and what would i be doing wrong then,its the same that i have i guess – Venky Apr 22 '17 at 10:05
  • I think there us a exception encountered in your try catch block and thus every time your doInBackground returns null , try returning something else like return"Test1" and in postExecute Toast the result – Dwijraj Bhattacharyya Apr 22 '17 at 10:25
0
public static Retrofit getRetro() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request request = chain
                            .request()
                            .newBuilder()
                            .build();
                    return chain.proceed(request);
                }
            }).build();

    return new Retrofit.Builder()
            .baseUrl(Global.DOT_NET_IP)//ur link like http://google.com
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();
}


public void setData() {
    Call<JsonElement> call = getRetro().create(RestApi.class).setData("SERVICE_FORGOT_USER_PASSWORD", "7878009686", "gf", "2");

    call.enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {

            Log.e(TAG, "onResponse: " + response.body().toString());//ur response
        }

        @Override
        public void onFailure(Call<JsonElement> call, Throwable t) {

        }
    });
}

RestApi.class

@GET("set_data_url")
Call<JsonElement> setData(
        @Query("REQUEST_TYPE_SENT") String REQUEST_TYPE_SENT,
        @Query("phone") String phone,
        @Query("device_token") String device_token,
        @Query("device_type") String device_type);
Rajesh Gauswami
  • 572
  • 7
  • 22