asynctaskloader android attempt

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

asynctaskloader android attempt

Postby jello » Fri Apr 19, 2013 4:43 pm

I'm writing a twitter android app, using OAuth. when I do
Code: Select all
consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
, I believe it tries to access the internet. Since you can't access the internet on the main UI thread, I have to use asynctaskloader. That's what I'm having trouble with. I'm not sure how exactly to do it. Here's my attempt:

Code: Select all
public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks{

   final String TAG = getClass().getName();
   
   private OAuthConsumer consumer;
    private OAuthProvider provider;
    private String token;
    private String secret;
    private getCommonsHttpOAuthConsumer consumerLoader;
   
    Activity context;
   
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      Button signInButton = (Button) findViewById(R.id.signInButton);
       
      signInButton.setOnClickListener(new View.OnClickListener() {
         
         public void onClick(View v) {
            // TODO Auto-generated method stub
            sign_in();
         }
      });      
   }

   
   public void sign_in()
    {
       try {
          
          Log.i(TAG, "test");
          
          this.consumer = (OAuthConsumer) getSupportLoaderManager().initLoader(0, null, this); //here's the error
          
          Log.i(TAG, "test2");
       } catch (Exception e) {
          Log.e(TAG, "Error creating consumer / provider",e);
       }       
    }
   
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
   
   
   @Override
    public android.support.v4.content.Loader onCreateLoader(int id, Bundle arg1) {
      context = this;
      
      consumerLoader = new getCommonsHttpOAuthConsumer(context);
        return consumerLoader;
    }


   @Override
   public void onLoadFinished(android.support.v4.content.Loader arg0,
         Object arg1) {
      // TODO Auto-generated method stub
      
   }


   @Override
   public void onLoaderReset(android.support.v4.content.Loader arg0) {
      // TODO Auto-generated method stub
      
   }
   
}





class getCommonsHttpOAuthConsumer extends AsyncTaskLoader<OAuthConsumer>{

   public getCommonsHttpOAuthConsumer(Context context) {
      super(context);
      // TODO Auto-generated constructor stub
   }

   @Override
   public OAuthConsumer loadInBackground() {
      // TODO Auto-generated method stub
      
      return new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
   }
   
}


I get a java.lang.ClassCastException on the line: this.consumer = (OAuthConsumer) getSupportLoaderManager().initLoader(0, null, this);. I know why I'm getting that error; it's because I'm trying to cast a loader to OAuthConsumer. But I don't know how else to write the code in order to compile. So what am I doing wrong?
jello
Freshman
Freshman
 
Posts: 2
Joined: Mon Apr 08, 2013 6:24 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 2 guests