2016-06-13 2 views
0

J'ai une activité qui a quatre fragments. Je veux accéder à la variable d'un asyncTask de cette activité. La variable est instanciée dans la méthode onPostExecute d'une asyncTask.Impossible d'accéder à la variable d'un asyncTask

J'ai essayé de créer une méthode dans cette classe et d'y accéder dans une activité. mais je reçois l'objet nul.

Aucune idée Ce qui ne va pas.

AsyncTask

public class GetProfileAsyncTask extends AsyncTask<String, Void, JSONObject> { 
    String api; 
    JSONObject jsonParams; 
    private Context context; 
    public static JSONObject profile; 

    public GetProfileAsyncTask(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected JSONObject doInBackground(String... params) { 
     try { 
      api = context.getResources().getString(R.string.server_url) + "api/user/getprofile.php"; 

      jsonParams = new JSONObject(); 
      String username = params[0]; // params[0] is username 
      jsonParams.put("user", username); 

      ServerRequest request = new ServerRequest(api, jsonParams); 
      return request.sendRequest(); 
     } catch(JSONException je) { 
      return Excpetion2JSON.getJSON(je); 
     } 
    } //end of doInBackground 

    @Override 
    protected void onPostExecute(JSONObject response) { 
     super.onPostExecute(response); 
     Log.e("ServerResponse", response.toString()); 
     try { 
      int result = response.getInt("result"); 
      profile = response.getJSONObject("profile"); 

      String message = response.getString("message"); 
      if (result == 0) { 
       Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
       //code after getting profile details goes here 
      } else { 
       Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
       //code after failed getting profile details goes here 
      } 
     } catch(JSONException je) { 
      je.printStackTrace(); 
      Toast.makeText(context, je.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } //end of onPostExecute 

    public static JSONObject getProfile() 
    { 
     return profile; 
    } 
} 

Activité

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

    toolbar.setTitle("Profile"); 

    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp); 


    setSupportActionBar(toolbar); 

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      finish(); 

     } 
    }); 

    profileAsyncTask = new GetProfileAsyncTask(this); 

    GetProfileAsyncTask task = new GetProfileAsyncTask(this); 

    profile = GetProfileAsyncTask.getProfile(); 

    task.execute("sid"); 

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mTitles,mNumbOfTabs); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager)findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    mTabs = (SlidingTabLayout)findViewById(R.id.tabs); 
    mTabs.setDistributeEvenly(true); 
    mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
     @Override 
     public int getIndicatorColor(int position) { 
      return getResources().getColor(R.color.tab_scroll_color); 
     } 
    }); 

    mTabs.setViewPager(mViewPager); 
} 

EDIT: Essayé ce encore son nul.

Comment accéder aux paramètres à partir d'un objet JSON?

En AsyncTask:

public void onTaskCompleted(JSONObject response) { 
    profile = response; 
} 

Activité:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

    toolbar.setTitle("Profile"); 

    toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp); 


    setSupportActionBar(toolbar); 

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      finish(); 

     } 
    }); 

    profileAsyncTask = new GetProfileAsyncTask(this); 

    GetProfileAsyncTask task = new GetProfileAsyncTask(this); 
    task.execute("sid"); 

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),mTitles,mNumbOfTabs); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager)findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    mTabs = (SlidingTabLayout)findViewById(R.id.tabs); 
    mTabs.setDistributeEvenly(true); 
    mTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
     @Override 
     public int getIndicatorColor(int position) { 
      return getResources().getColor(R.color.tab_scroll_color); 
     } 
    }); 

    mTabs.setViewPager(mViewPager); 
} 

@Override 
public void onTaskCompleted(JSONObject response) { 
    profile = response; 
} 

Obtenir objet profil comme nul. S'il vous plaît aider. Je vous remercie.

+1

votre asynctask est asynchrone. Vous pouvez utiliser l'interface comme un rappel à l'activité – Raghunandan

+0

comment? @Raghunandan –

+0

juste google vous trouvez beaucoup de réponses stackvoerflow. en voici un https://xelsoft.wordpress.com/2014/11/28/asynctask-implementation-using-callback-interface/ – Raghunandan

Répondre

0

Créez le champ en dehors de la tâche au niveau de l'activité. Mettez-le à jour dans onPostExecute.

+0

ok j'ai suivi ce lien: stackoverflow.com/questions/9963691/ ..., que faire dans une activité? comment obtenir cette variable dans une activité? @usajnf –