2012-12-18 5 views
0

Android 애플리케이션에 Salesforce OAuth를 구현하려고합니다. 구현을 위해 Salesforce Mobile SDK를 사용했습니다. 내 프로젝트에 'SalesforceSDK-1.0.1.jar'도 추가했습니다.Salesforce 용 안드로이드에 Oauth를 구현하는 방법은 무엇입니까?

MainActivity.java 내가 로그 캣 오류 다음 얻고있다

package com.android.templateapp; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import com.salesforce.androidsdk.app.ForceApp; 
import com.salesforce.androidsdk.rest.ClientManager; 
import com.salesforce.androidsdk.rest.ClientManager.LoginOptions; 
import com.salesforce.androidsdk.rest.ClientManager.RestClientCallback; 
import com.salesforce.androidsdk.rest.RestClient; 


public class MainActivity extends Activity { 

private RestClient client; 
private ArrayAdapter<String> listAdapter; 
private static String TAG = "TemplateApp"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.v(TAG," onCreate of Main Activity"); 
    // Setup view 
    setContentView(R.layout.main); 
} 

@Override 
public void onResume() { 
    Log.v(TAG," ******* default onResume of Main Activity"); 
     super.onResume(); 

     findViewById(R.id.root).setVisibility(View.INVISIBLE); 
     Log.v(TAG," ******* default onResume - View.INVISIBLE"); 

     // Login options 
     String accountType = ForceApp.APP.getAccountType(); 

     Log.v(TAG," ******* default onResume - accountType ::"+accountType); 

     LoginOptions loginOptions = new LoginOptions(
       "https://login.salesforce.com/", // 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"}); 
     Log.v(TAG," ******* default onResume - loginOptions ::"+loginOptions); 


     // Get a rest client 
     new ClientManager(this, accountType, loginOptions).getRestClient(this, 
         new RestClientCallback() { 
      @Override 
      public void authenticatedRestClient(RestClient client) { 

       // Show everything 
       findViewById(R.id.root).setVisibility(View.VISIBLE); 

       //User is authenticated, insert Application logic here 
      } 
     }); 
    } 


protected LoginOptions getLoginOptions() { 
    Log.v(TAG," getLoginOptions of Main Activity"); 
    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"}); 
    return loginOptions; 
} 

public void onResume(RestClient client) { 
    Log.v(TAG," onResume of Main Activity"); 
    // Keeping reference to rest client 
    this.client = client; 

    // Show everything 
    findViewById(R.id.root).setVisibility(View.VISIBLE); 
} 

/** 
* Called when "Logout" button is clicked. 
* 
* @param v 
*/ 
public void onLogoutClick(View v) { 
    ForceApp.APP.logout(this); 
} 

/** 
* Called when "Clear" button is clicked. 
* 
* @param v 
*/ 

public void onClearClick(View v) { 
    listAdapter.clear(); 
} 

/** 
* Called when "Fetch Contacts" button is clicked 
* 
* @param v 
* @throws UnsupportedEncodingException 
*/ 

public void onFetchContactsClick(View v) throws UnsupportedEncodingException { 
    sendRequest("SELECT Name FROM Contact"); 
} 

public void onFetchAccountsClick(View v) throws UnsupportedEncodingException { 
    sendRequest("SELECT Name FROM Account"); 
} 

private void sendRequest(String soql) throws UnsupportedEncodingException { 
    RestRequest restRequest = RestRequest.getRequestForQuery(getString(R.string.api_version), soql); 

    client.sendAsync(restRequest, new AsyncRequestCallback() { 
     @Override 
     public void onSuccess(RestRequest request, RestResponse result) { 
      try { 
       listAdapter.clear(); 
       JSONArray records = result.asJSONObject().getJSONArray("records"); 
       for (int i = 0; i < records.length(); i++) { 
        listAdapter.add(records.getJSONObject(i).getString("Name")); 
       }     
      } catch (Exception e) { 
       onError(e); 
      } 
     } 

     @Override 
     public void onError(Exception exception) { 
      Toast.makeText(MainActivity.this, 
          MainActivity.this.getString(ForceApp.APP.getSalesforceR().stringGenericError(), exception.toString()), 
          Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

} 

: AndroidManifest를에

12-18 17:47:04.696: E/Trace(1586): error opening trace file: No such file or directory (2) 
12-18 17:47:05.256: V/TemplateApp(1586): onCreate of Main Activity 
12-18 17:47:05.526: V/TemplateApp(1586): ******* default onResume of Main Activity 
12-18 17:47:05.526: V/TemplateApp(1586): ******* default onResume - View.INVISIBLE 
12-18 17:47:05.536: D/AndroidRuntime(1586): Shutting down VM 
12-18 17:47:05.536: W/dalvikvm(1586): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
12-18 17:47:05.546: E/AndroidRuntime(1586): FATAL EXCEPTION: main 
12-18 17:47:05.546: E/AndroidRuntime(1586): java.lang.RuntimeException: Unable to resume activity {com.android.templateapp/com.android.templateapp.MainActivity}: java.lang.NullPointerException 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.os.Looper.loop(Looper.java:137) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at dalvik.system.NativeStart.main(Native Method) 
12-18 17:47:05.546: E/AndroidRuntime(1586): Caused by: java.lang.NullPointerException 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at com.salesforce.androidsdk.app.ForceApp.getAccountType(ForceApp.java:175) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at com.android.templateapp.MainActivity.onResume(MainActivity.java:85) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.Activity.performResume(Activity.java:5082) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565) 
12-18 17:47:05.546: E/AndroidRuntime(1586):  ... 12 more 
+0

감사 @Asmita하지만 난 당신이 그것을에 관한 몇 가지 유용한 공유하시기 바랍니다 수 있습니다, 응용 프로그램에서 ForceApp을 가져 오는 방법을 알고 싶어요. 미리 감사드립니다. –

+0

@Asmita. .... 해결책을 찾으면 알려주세요. –

답변

2

난 당신이하지 설치 ForceApp을 확장하는 애플리케이션 클래스가 상상할 것 등록하는 것이 .xml 파일.

+0

ForceApp을 조금 불편하게합니다. 자체 정적 참조를 유지하는 이유는 무엇입니까? 왜 그들은'Main (활동)'생명주기를 공중 납치합니까? 도용을 파편에 위임해야합니까? Blagh 나는 총알을 맞을 것이지만 개발자들에 대한 생각을 강요하는 SDK를 감상하는 것은 어렵다. – edthethird

+0

AndroidManifest.xml에 ForceApp 하위 클래스를 등록하는 방법은 무엇입니까? 어디에서나 그 정보를 찾을 수 없으며 템플릿 코드의 AndroidManifest.xml에 ForceApp 하위 클래스에 대한 언급이 없습니다. 감사! – Patrick

+0

표준 안드로이드 응용 프로그램 클래스이므로 안드로이드 : manifest의 요소에있는 클래스 이름에 android : name을 설정하십시오. – superfell

0

당신은 모바일 앱에 대한 액세스 권한을 부여하기 위해 귀하의 영업 인력의 조직에 대한 원격 액세스를 제공해야합니다 사용하여 OAuth

http://wiki.developerforce.com/page/Getting_Started_with_the_Mobile_SDK_for_Android#Authentication

인증에 대해 다음 URL을 참조하십시오. oAuth 리디렉션 URL & oauth 로그인 도메인 URL과 함께 응용 프로그램에서 원격 액세스 사용자 키를 지정해야합니다.

+0

'ClientManager'는 더 이상 사용되지 않으며 대안이없는 것 같습니까? – edthethird

0

동일한 오류가 발견되어 수퍼 펠의 답변이 도움이되었습니다. sample TemplateApp application을 보면 TemplateApp.java가 ForceApp를 확장합니다. 내가 좋아하는 내 매니페스트 파일에 추가하여 오류를 제거 할 수 있었다 : 질문에 대한

<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".TemplateApp"> 
관련 문제