Twitter4j Error 401


#1

I know this question has been asked by many but still cant get a solution. I have an android app that should enable a user post a tweet but im getting a 401 and dont get why. Please help
Code is

package tweetie.twitterapp.com.twitterapp;

/**
 * Created by Sheldon Rotich on 2/4/2015.
 */

import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;

public class LoginFragment extends Fragment {
    ImageView login;
    Twitter twitter;
    RequestToken requestToken = null;
    AccessToken accessToken;
    String oauth_url,oauth_verifier,profile_url;
    Dialog auth_dialog;
    WebView web;
    SharedPreferences pref;
    ProgressDialog progress;
    Bitmap bitmap;
    String TAG = LoginFragment.class.getSimpleName();
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_fragment, container, false);
        login = (ImageView)view.findViewById(R.id.login);
        pref = getActivity().getPreferences(0);


        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setDebugEnabled(true);
        builder.setOAuthConsumerKey(pref.getString("CONSUMER_KEY", ""));
        builder.setOAuthConsumerSecret(pref.getString("CONSUMER_SECRET",""));
        builder.setOAuthAccessToken(null);
        builder.setOAuthAccessTokenSecret(null);


        TwitterFactory factory = new TwitterFactory(builder.build());
        twitter = factory.getInstance();

//        twitter = new TwitterFactory().getInstance();
//        Toast.makeText(getActivity(),"Key is " + pref.getString("CONSUMER_KEY","") + "Secret is " + pref.getString("CONSUMER_SECRET",""),Toast.LENGTH_LONG).show();
//        twitter.setOAuthConsumer(pref.getString("CONSUMER_KEY", ""), pref.getString("CONSUMER_SECRET", ""));
        login.setOnClickListener(new LoginProcess());
        return view;
    }
    private class LoginProcess implements OnClickListener {

        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(),"Pressed",Toast.LENGTH_LONG).show();
            // TODO Auto-generated method stub
            new TokenGet().execute();
        }}
    private class TokenGet extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... args) {
            try {
                //Toast.makeText(getActivity(),"Do in Background",Toast.LENGTH_LONG).show();
//                requestToken = twitter.getOAuthRequestToken();
//                Log.d(TAG, "Sheldon the RequestToken is " + requestToken);
//                oauth_url = requestToken.getAuthorizationURL();


                requestToken = twitter.getOAuthRequestToken("http://tweetie.com");
                Log.d(TAG, "Sheldon the RequestToken is " + requestToken);
                oauth_url = requestToken.getAuthorizationURL();

                //End
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d(TAG,"Sheldon the OAUTH URL is " +  oauth_url);
            return oauth_url;

        }

        @Override
        protected void onPostExecute(String oauth_url) {

            if(oauth_url != null){
                Log.e("URL", oauth_url);
                auth_dialog = new Dialog(getActivity());
                auth_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                auth_dialog.setContentView(R.layout.auth_dialog);
                web = (WebView)auth_dialog.findViewById(R.id.webv);
                web.getSettings().setJavaScriptEnabled(true);
                web.loadUrl(oauth_url);
                web.setWebViewClient(new WebViewClient() {
                    boolean authComplete = false;
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon){
                        super.onPageStarted(view, url, favicon);
                    }
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        if (url.contains("oauth_verifier") && authComplete == false){
                            authComplete = true;
                            Log.e("Url",url);
                            Uri uri = Uri.parse(url);
                            oauth_verifier = uri.getQueryParameter("oauth_verifier");
                            auth_dialog.dismiss();
                            new AccessTokenGet().execute();
                        }else if(url.contains("denied")){
                            auth_dialog.dismiss();
                            Toast.makeText(getActivity(), "Sorry !, Permission Denied", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                auth_dialog.show();
                auth_dialog.setCancelable(true);
            }else{
                Toast.makeText(getActivity(), "Sorry !, Network Error or Invalid Credentials", Toast.LENGTH_SHORT).show();
            }
        }
    }
    private class AccessTokenGet extends AsyncTask<String, String, Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress = new ProgressDialog(getActivity());
            progress.setMessage("Fetching Data ...");
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.show();
        }
        @Override
        protected Boolean doInBackground(String... args) {
            try {
                accessToken = twitter.getOAuthAccessToken(requestToken, oauth_verifier);
                SharedPreferences.Editor edit = pref.edit();
                edit.putString("ACCESS_TOKEN", accessToken.getToken());
                edit.putString("ACCESS_TOKEN_SECRET", accessToken.getTokenSecret());
                User user = twitter.showUser(accessToken.getUserId());
                profile_url = user.getOriginalProfileImageURL();
                edit.putString("NAME", user.getName());
                edit.putString("IMAGE_URL", user.getOriginalProfileImageURL());
                edit.commit();
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return true;
        }
        @Override
        protected void onPostExecute(Boolean response) {
            if(response){
                progress.hide();
                Fragment profile = new ProfileFragment();
                FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
                ft.replace(R.id.content_frame, profile);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.addToBackStack(null);
                ft.commit();
            }
        }
    }
}