Salesforce的REST API与Android - NullPointerException异常@ AsyncRequestCallback异常、API、REST、Salesforce

2023-09-04 05:04:54 作者:淡紫幽香

我试图让Salesforce的REST API与Android和新到Android编程工作,随后将样品code与SFDC http://wiki.developerforce.com/page/Getting_Started_with_the_Mobile_SDK_for_Android#Authentication

I'm trying to get the Salesforce REST API working with Android and new to android programming, followed the sample code to connect with SFDC http://wiki.developerforce.com/page/Getting_Started_with_the_Mobile_SDK_for_Android#Authentication

我试图从SFDC得到一些记录,并在Android应用程序中显示它们,看起来像在异步调用时在client.sendAsync(sfRequest,新AsyncRequestCallback() - 抛出NullPointerException异常

I'm trying to get a few records from SFDC and display them in the android app, looks like when the Async Call is made at "client.sendAsync(sfRequest, new AsyncRequestCallback()" - NullPointerException is thrown.

我确实看到了几个类似的问题在网上,但没有帮助我。希望如果有一个人会点我在正确的方向来解决此。谢谢了。

I did see a couple of similar issues online, but didn't help me. Hoping if some one would point me in the right direction to troubleshoot this. Thanks much.

    public class GetAccountsActivity extends Activity {

    private PasscodeManager passcodeManager;

    private String soql;
    private String apiVersion;
    private RestClient client;
    private TextView resultText;
    private RestRequest sfRequest;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get Api Version
        apiVersion = getString(R.string.api_version);
      //Create Query
        soql = "select id, name from Account limit 10";

      //  Setup view
        setContentView(R.layout.get_accounts_activity);
        ((TextView) findViewById(R.id.Acc_Title)).setText(apiVersion);
        // Passcode manager

        passcodeManager = ForceApp.APP.getPasscodeManager();
    }

    @Override 
    public void onResume() {
        super.onResume();   

    //Get SFClient
        // Login options
        String accountType = ForceApp.APP.getAccountType();
        LoginOptions loginOptions = new LoginOptions(
                null, // login host is chosen by user through the server picker 
                ForceApp.APP.getPasscodeHash(),
                getString(R.string.oauth_callback_url),
                getString(R.string.oauth_client_id),
                new String[] {"api"});

        new ClientManager(this, accountType, loginOptions).getRestClient(this, new RestClientCallback() {
            @Override
            public void authenticatedRestClient(RestClient client) {
                if (client == null) {
                    ForceApp.APP.logout(GetAccountsActivity.this);
                    return;
                }
                GetAccountsActivity.this.client = client;
            }
        });
          //Get Rest Object to query
            try {
                sfRequest = RestRequest.getRequestForQuery(apiVersion, soql);
                //Use SF Rest Client to send the request
                client.sendAsync(sfRequest, new AsyncRequestCallback(){
                    @Override
                    public void onSuccess(RestRequest request, RestResponse response){
                    //Check responses and display results
                    // EventsObservable.get().notifyEvent(EventType.RenditionComplete);
                    }//end onSuccess

                    @Override
                    public void onError(Exception exception) {
                        //printException(exception);
                        EventsObservable.get().notifyEvent(EventType.RenditionComplete);                
                    }//End Exception for Async Method
                });
            }catch (UnsupportedEncodingException e) {
                //printHeader("Could Send Query request");
                //printException(e);
                return;
            }
    }
}

在此输入code

enter code here

推荐答案

您在呼唤client.sendAsync从onResume(),但客户没有设置直到authenticatedRestClient回调被调用时,你需要将你的sendAsync调用到authenticatedRestClient回调

You are calling client.sendAsync from onResume() but client is not set until the authenticatedRestClient callback is called, you need to move your sendAsync call into the authenticatedRestClient callback.