插入端点不起作用

时间:2016-04-18 16:08:22

标签: android google-app-engine google-cloud-endpoints objectify google-authentication

所以我创建了一个可以插入用户的端点。但由于某种原因它不起作用因为每次我检查索引它是空的。

以下是端点的后端代码。

@ApiMethod(
            name = "insert",
            path = "person",
            httpMethod = ApiMethod.HttpMethod.POST,
            clientIds =  {Constants.WEB_CLIENT_ID,
                         Constants.ANDROID_AUDIENCE, Constant.API_EXPLORER_CLIENT_ID},
              audiences = {Constants.ANDROID_AUDIENCE})
    public Person insert(final User user ,final PersonForm personForm ) throws UnauthorizedException {

        if(user == null){
            throw new UnauthorizedException("You need to authorisation");
        }






        Person person = ofy().load().type(Person.class).id(user.getUserId()).now();
        if(person == null){
            String id = personForm.getId();
            String name = personForm.getName();
            String email = personForm.getEmail();


            person = new Person(id,name,email);

        }





        ofy().save().entity(person).now();
        logger.info("Created Person with ID: " + person.getUserId());

        return ofy().load().entity(person).now();
    }

以下是使用的saveprofile异步任务;

public class SaveProfile extends AsyncTask<String, Void, Void> {

    private static PersonApi PersonApiService = null;
    private Context context;


    @Override
    protected Void doInBackground(String... params) {

        service();

        try {


            PersonForm personForm = new PersonForm();
            personForm.setId(params[0]);
            personForm.setName(params[1]);
            personForm.setEmail(params[2]);



            PersonApiService.insert(personForm).execute();



        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    private void service() {
        if (PersonApiService == null) {
            PersonApi.Builder builder = new PersonApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    .setRootUrl("http://192.168.0.3:8080/_ah/api/")
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> request) throws IOException {
                            request.setDisableGZipContent(true);
                        }
                    });

            PersonApiService = builder.build();

        }

    }

}

最后这里是登录活动,我通过上面的异步任务实际执行端点。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        findViewById(R.id.Sign_In_Button).setOnClickListener(this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestIdToken(WEB_CLIENT_ID)
                .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Sign_In_Button:
                signIn();
                break;

        }
    }


    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

    private void handleSignInResult(GoogleSignInResult result) {
        Log.d(TAG, "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.

            GoogleSignInAccount acct = result.getSignInAccount();
            tempName = acct.getDisplayName();
            tempEmail = acct.getEmail();
            tempID = acct.getId();
            PersonForm personForm = new PersonForm();



            SaveProfile saveProfile = new SaveProfile();
            String[] params = {tempID, tempName, tempEmail};

            saveProfile.execute(params);

            Toast.makeText(this, tempName,Toast.LENGTH_LONG).show();









        } else {
            // Signed out, show unauthenticated UI.
            Toast.makeText(this, "Currently Signed In", Toast.LENGTH_SHORT).show();
        }
    }

}

因此,每次运行它时,后端都不会出现任何实际情况。

感谢任何帮助

0 个答案:

没有答案