如何在android中将doinbackground响应传递给Onpostexecute

时间:2017-04-22 09:44:11

标签: android android-asynctask

这是我的Doinbackground,由于某些原因我在postexecute的String中找不到任何东西,我做错了什么?,顺便通过new mytask()调用这个asynctask。执行(url,dum)来自另一个类

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

4 个答案:

答案 0 :(得分:1)

而不是这个

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

尝试使用此

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

答案 1 :(得分:0)

您必须首先参考page如何使用异步任务...

答案 2 :(得分: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) {}
}

答案 3 :(得分: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);