未检索到图片网址

时间:2015-10-11 00:08:30

标签: android

通过从google plus中检索用户数据,制作一个为gcm服务注册用户的应用。该应用程序能够检索数据并在服务器中注册。一旦注册,用户信息就存储在意图中并传递给下一个活动。但是,由于无法检索要传递的图像URL,应用程序会开始下一个活动。毕加索。请帮助

LOG CAT

10-11 02:33:50.427  16208-16208/com.smartdevelopers.kandie.nicedrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.smartdevelopers.kandie.nicedrawer, PID: 16208
    java.lang.NoSuchMethodError: com.squareup.okhttp.OkHttpClient.setWriteTimeout
            at com.squareup.picasso.OkHttpDownloader.defaultOkHttpClient(OkHttpDownloader.java:34)
            at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:76)
            at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:55)
            at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:45)
            at com.squareup.picasso.Utils$OkHttpLoaderCreator.create(Utils.java:424)
            at com.squareup.picasso.Utils.createDefaultDownloader(Utils.java:250)
            at com.squareup.picasso.Picasso$Builder.build(Picasso.java:832)
            at com.squareup.picasso.Picasso.with(Picasso.java:662)
            at com.smartdevelopers.kandie.nicedrawer.NavigationDrawerFragment.setUserData(NavigationDrawerFragment.java:257)
            at com.smartdevelopers.kandie.nicedrawer.MainActivity.onCreate(MainActivity.java:66)
            at android.app.Activity.performCreate(Activity.java:5343)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
            at 
            4)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

ActivityGplus.java

package com.smartdevelopers.kandie.nicedrawer;

/**
 * Created by 4331 on 14/07/2015.
 */
public class ActivityGplus extends AppCompatActivity implements View.OnClickListener,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private static final int RC_SIGN_IN = 0;
    // Logcat tag
    private static final String TAG = "ActivityGplus";

    // Profile pic image size in pixels
    private static final int PROFILE_PIC_SIZE = 400;

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;

    private boolean mIntentInProgress;

    private boolean mSignInClicked;

    private ConnectionResult mConnectionResult;

    private SignInButton btnSignIn;
    private Button btnEnter;

    String personPhotoUrl;
    String personName;
    String email;

    GoogleCloudMessaging gcm;
    AtomicInteger msgId = new AtomicInteger();
    SharedPreferences prefs;
    Context context;
    String regid;
    String msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gplus_activity);
        //Cloud Messaging
        if(isUserRegistered(context)){
            startActivity(new Intent(ActivityGplus.this,MainActivity.class));
            finish();
        }else {
            // Initializing google plus api client
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(Plus.API)
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();
                /*
        Adding KenBurns View
        */
            KenBurnsView kbv = (KenBurnsView) findViewById(R.id.imageBurn);
            kbv.setTransitionListener(new KenBurnsView.TransitionListener() {
                @Override
                public void onTransitionStart(Transition transition) {
                }

                @Override
                public void onTransitionEnd(Transition transition) {

                }
            });
            btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
            btnEnter=(Button) findViewById(R.id.btn_main);
            context = getApplicationContext();

            // Button click listeners
            btnSignIn.setOnClickListener(this);
            // Check device for Play Services APK. If check succeeds, proceed with
            //  GCM registration.

            if (checkPlayServices()) {
                gcm = GoogleCloudMessaging.getInstance(this);
                regid = getRegistrationId(context);

                if (regid.isEmpty()) {
                    registerInBackground();
                }


            } else {
                Log.i("pavan", "No valid Google Play Services APK found.");
            }
        }

    }

    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }


    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sign_in:
                // Signin button clicked
                signInWithGplus();
                break;
            case R.id.btn_main:
                //enter to the main activity
                enterMain();
                break;

        }
    }


    private void enterMain() {
        Intent intent=new Intent(getApplicationContext(),com.smartdevelopers.kandie.nicedrawer.MainActivity.class);
        startActivity(intent);
    }

    private void revokeGplusAccess() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
                    .setResultCallback(new ResultCallback<Status>() {
                        @Override
                        public void onResult(Status arg0) {
                            Log.e(TAG, "User access revoked!");
                            mGoogleApiClient.connect();
                            updateUI(false);
                        }

                    });
        }

    }

    private void signOutFromGplus() {
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            updateUI(false);
        }
    }

    private void signInWithGplus() {
        if (!mGoogleApiClient.isConnecting()) {
            mSignInClicked = true;
            resolveSignInError();
        }

    }

    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (IntentSender.SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (!connectionResult.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this,
                    0).show();
            return;
        }

        if (!mIntentInProgress) {
            // Store the ConnectionResult for later usage
            mConnectionResult = connectionResult;

            if (mSignInClicked) {
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError();
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            if (resultCode != RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnected(Bundle bundle) {
        mSignInClicked = false;
        // Get user's information
        getProfileInformation();

        // Update the UI after signin
        updateUI(true);
    }

    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
        updateUI(false);

    }

    /**
     * Updating the UI, showing/hiding buttons and profile layout
     * */
    private void updateUI(boolean isSignedIn) {
        if (isSignedIn) {
            btnSignIn.setVisibility(View.GONE);
            btnEnter.setVisibility(View.VISIBLE);

        } else {
            btnSignIn.setVisibility(View.VISIBLE);
            btnEnter.setVisibility(View.GONE);
        }
    }

    /**
     * Fetching user's information name, email, profile pic
     * */
    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                personName = currentPerson.getDisplayName();
                personPhotoUrl = currentPerson.getImage().getUrl();
                String personGooglePlusProfile = currentPerson.getUrl();
                email = Plus.AccountApi.getAccountName(mGoogleApiClient);

                Log.e(TAG, "Name: " + personName + ", plusProfile: "
                        + personGooglePlusProfile + ", email: " + email
                        + ", Image: " + personPhotoUrl);

                personPhotoUrl = personPhotoUrl.substring(0,
                        personPhotoUrl.length() - 2)
                        + PROFILE_PIC_SIZE;

                sendRegistrationIdToBackend();
                //putting extra information in the Intent
//                Intent intent=new Intent(ActivityGplus.this,MainActivity.class);
//                intent.putExtra("username",personName);
//                intent.putExtra("mail",email);
//                intent.putExtra("picture", personPhotoUrl);
//                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(),
                        "null", Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Google Cloud Messaging
     */
    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                        Util.PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Log.i(TAG, "This device is not supported.");
                finish();
            }
            return false;
        }
        return true;
    }
    private String getRegistrationId(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String registrationId = prefs.getString(Util.PROPERTY_REG_ID, "");
        if (registrationId.isEmpty()) {
            Log.i(TAG, "Registration not found.");
            return "";
        }
        // Check if app was updated; if so, it must clear the registration ID
        // since the existing registration ID is not guaranteed to work with
        // the new app version.
        int registeredVersion = prefs.getInt(Util.PROPERTY_APP_VERSION, Integer.MIN_VALUE);
        int currentVersion = getAppVersion(context);
        if (registeredVersion != currentVersion) {
            Log.i(TAG, "App version changed.");
            return "";
        }
        return registrationId;
    }


    private boolean isUserRegistered(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        String User_name = prefs.getString(Util.USER_NAME, "");
        if (User_name.isEmpty()) {
            Log.i(TAG, "Registration not found.");
            return false;
        }

        return true;
    }
    private void registerInBackground() {
        new AsyncTask() {
            @Override
            protected String doInBackground(Object[] params) {
                try {
                    if (gcm == null) {
                        gcm = GoogleCloudMessaging.getInstance(ActivityGplus.this);
                    }
                    regid = gcm.register(Util.SENDER_ID);
                    msg = "Device registered, registration ID=" + regid;
                    // You should send the registration ID to your server over HTTP,
                    //GoogleCloudMessaging gcm;/ so it can use GCM/HTTP or CCS to send messages to your app.
                    // The request to your server should be authenticated if your app
                    // is using accounts.
                    // sendRegistrationIdToBackend();

                    // For this demo: we don't need to send it because the device
                    // will send upstream messages to a server that echo back the
                    // message using the 'from' address in the message.

                    // Persist the registration ID - no need to register again.
                    storeRegistrationId(context, regid);
                } catch (IOException ex) {
                    msg = "Error :" + ex.getMessage();
                    // If there is an error, don't just keep trying to register.
                    // Require the user to click a button again, or perform
                    // exponential back-off.
                }
                return msg;
            }
        }.execute();

    }

    private static int getAppVersion(Context context) {
        try {
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // should never happen
            throw new RuntimeException("Could not get package name: " + e);
        }
    }

    private void storeRegistrationId(Context context, String regId) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(Util.PROPERTY_REG_ID, regId);
        editor.putInt(Util.PROPERTY_APP_VERSION, appVersion);
        editor.commit();
    }

    private void storeUserDetails(Context context) {
        final SharedPreferences prefs = getGCMPreferences(context);
        int appVersion = getAppVersion(context);
        Log.i(TAG, "Saving regId on app version " + appVersion);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(Util.EMAIL,email);
        editor.putString(Util.USER_NAME, personName);
        editor.commit();
    }
    private SharedPreferences getGCMPreferences(Context context) {
        // This sample app persists the registration ID in shared preferences, but
        // how you store the registration ID in your app is up to you.
        return getSharedPreferences(ActivityGplus.class.getSimpleName(),
                Context.MODE_PRIVATE);
    }
    //  private RequestQueue mRequestQueue;
    private void sendRegistrationIdToBackend() {
        // Your implementation here.
        new SendGcmToServer().execute();
// Access the RequestQueue through your singleton class.
        // AppController.getInstance().addToRequestQueue(jsObjRequest, "jsonRequest");
    }

    private class SendGcmToServer extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            String url = Util.register_url+"?name="+personName+"&email="+email+"&regId="+regid;
            Log.i("pavan", "url" + url);

            OkHttpClient client_for_getMyFriends = new OkHttpClient();

            String response = null;
            // String response=Utility.callhttpRequest(url);

            try {
                url = url.replace(" ", "%20");
                response = callOkHttpRequest(new URL(url),
                        client_for_getMyFriends);
                for (String subString : response.split("<script", 2)) {
                    response = subString;
                    break;
                }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            //Toast.makeText(context,"response "+result,Toast.LENGTH_LONG).show();

            if (result != null) {
                if (result.equals("success")) {
                    storeUserDetails(context);
                    Intent intent=new Intent(ActivityGplus.this,MainActivity.class);
                    intent.putExtra("username",personName);
                    intent.putExtra("mail", email);
                    intent.putExtra("picture", personPhotoUrl);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(context, "Try Again" + result, Toast.LENGTH_LONG).show();
                }
            }else{
                Toast.makeText(context, "Check net connection ", Toast.LENGTH_LONG).show();
            }
        }
    }
    // Http request using OkHttpClient
    String callOkHttpRequest(URL url, OkHttpClient tempClient)
            throws IOException {

        HttpURLConnection connection = tempClient.open(url);

        connection.setConnectTimeout(40000);
        InputStream in = null;
        try {
            // Read the response.
            in = connection.getInputStream();
            byte[] response = readFully(in);
            return new String(response, "UTF-8");
        } finally {
            if (in != null)
                in.close();
        }
    }

    byte[] readFully(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        for (int count; (count = in.read(buffer)) != -1;) {
            out.write(buffer, 0, count);
        }
        return out.toByteArray();
    }
}

MainActivity.java

 public class MainActivity extends ActionBarActivity
            implements NavigationDrawerCallbacks {
        private NavigationDrawerFragment mNavigationDrawerFragment;
        private Toolbar mToolbar;
        Intent intent;
        String user_name;
        String user_email;
        String user_photo_url;
        Fragment fragment;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
            setSupportActionBar(mToolbar);
            PreferenceManager.setDefaultValues(this,R.xml.settings,false);
            intent=getIntent();
            user_name=intent.getStringExtra("username");
            user_email=intent.getStringExtra("mail");
            user_photo_url=intent.getStringExtra("picture");
            mNavigationDrawerFragment = (NavigationDrawerFragment)
          getSupportFragmentManager().findFragmentById(R.id.fragment_drawer);
            mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
                mNavigationDrawerFragment.setUserData(user_name, user_email, user_photo_url);
    //        mNavigationDrawerFragment.setUserData("John Doe", "johndoe@doe.com", BitmapFactory.decodeResource(getResources(), R.drawable.avatar));
        }
        @Override
        public void onNavigationDrawerItemSelected(int position) {
            // update the main content by replacing fragments
            Fragment fragment;
            String title=getString(R.string.app_name);
            switch (position) {
                case 0: //News Fragment//todo
                    fragment=new NewsFragment();                 fragment=getSupportFragmentManager().findFragmentByTag(NewsFragment.TAG);
                    if (fragment==null){
                        fragment=new NewsFragment();
                    }getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,NewsFragment.TAG).commit();
                    break;
                case 1: //Politics Fragment//todo
                    fragment=new PoliticsFragment();
                    fragment = getSupportFragmentManager().findFragmentByTag(PoliticsFragment.TAG);
                    if (fragment == null) {
                        fragment = new PoliticsFragment();
                    }         getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment, PoliticsFragment.TAG).commit();
                    break;
                case 2: //Sports Fragment//todo
                    fragment=new SportsFragment();               fragment=getSupportFragmentManager().findFragmentByTag(SportsFragment.TAG);
                    if(fragment==null){
                        fragment=new SportsFragment();
                    }                getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,SportsFragment.TAG).commit();
                    break;
                case 3: //Music Fragment//todo
                    fragment=new MusicFragment();               fragment=getSupportFragmentManager().findFragmentByTag(MusicFragment.TAG);
                    if(fragment==null){
                        fragment=new MusicFragment();
                    }               getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,MusicFragment.TAG).commit();
    //                Intent intent=new Intent(getApplicationContext(),com.smartdevelopers.kandie.nicedrawer.TabsActivity.class);
    //                startActivity(intent);
                    break;
                case 4: //Art and Culture  Fragment//todo
                    fragment=new ArtandCultureFragment();                  fragment=getSupportFragmentManager().findFragmentByTag(ArtandCultureFragment.TAG);
                    if(fragment==null){
                        fragment=new ArtandCultureFragment();
                    }             getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,ArtandCultureFragment.TAG).commit();
                    break;
                case 5: //Agriculture Fragment//todo
                    fragment=new AgricultureFragment();
fragment=getSupportFragmentManager().findFragmentByTag(AgricultureFragment.TAG);
                    if(fragment==null){
                        fragment=new AgricultureFragment();
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,AgricultureFragment.TAG).commit();
                    break;
                case 6: //Counties Fragment//todo
                    fragment=new CountiesFragment();       fragment=getSupportFragmentManager().findFragmentByTag(CountiesFragment.TAG);
                    if(fragment==null){
                        fragment=new CountiesFragment();
                    }
            getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,CountiesFragment.TAG).commit();
                    break;
                case 7: //Counties Fragment//todo
                    fragment=new NotificationFragment();
                    fragment=getSupportFragmentManager().findFragmentByTag(NotificationFragment.TAG);
                    if(fragment==null){
                        fragment=new NotificationFragment();
                    }                 getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment,NotificationFragment.TAG).commit();
                    break;
                default:
                    break;
            }
        }
        @Override
        public void onBackPressed() {
            if (mNavigationDrawerFragment.isDrawerOpen())
                mNavigationDrawerFragment.closeDrawer();
            else
                super.onBackPressed();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            if (!mNavigationDrawerFragment.isDrawerOpen()) {
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }
            return super.onCreateOptionsMenu(menu);
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            switch (id){
                case R.id.menu_favorites:Bundle data_Bundle=new Bundle();
                    data_Bundle.putInt("id", 0);
                    //Refresh content
                    Intent intent=new Intent(getApplicationContext(),com.smartdevelopers.kandie.nicedrawer.ReadArticleActivity.class);
                    intent.putExtras(data_Bundle);
                    startActivity(intent);
                    break;
                case R.id.action_settings:Bundle bundle=new Bundle();
                    bundle.putInt("id", 1);
                    Intent i=new Intent(getApplicationContext(),com.smartdevelopers.kandie.nicedrawer.SettingsFragment.class);
                    i.putExtras(bundle);
                    startActivity(i);
                    break;
                default:
                    break;
            }
            if(fragment!=null)    this.getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
            else
            return super.onOptionsItemSelected(item);
        }
    }

1 个答案:

答案 0 :(得分:0)

您似乎遇到了错误here

java.lang.NoSuchMethodError