2015-11-04 43 views
14

我正在使用改造2一個Android應用程序。我的REST API全部用Liferay編寫。現在在Liferay中,我看到的是訪問我們首先需要進行身份驗證的Web服務。所以我有驗證這樣登錄於Android使用改造工作不

http://test:[email protected]:8080/liferay-portlet/api/secure/jsonws/ 

Liferay的有,我們有overridden.I檢查從郵差Web服務調用它的做工精細自己的用戶身份驗證方法。

URL:http://test:[email protected]:8080/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name 

形式編碼的值

companyId:10154 
screenName:xyz 
password:xyz 
active:true 

如果我把這個在郵遞員,它正確地取出JSON響應。

現在,當我打電話一樣從我的Android代碼我得到迴應「未授權」。

我的改造服務

public interface LoginApi {  
    @FormUrlEncoded 
    @POST("/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name") 
    Call<User> login(@Field("companyId")long companyId,@Field("screenName")String screenName,@Field("password")String password,@Field("active")boolean active); 
} 

我RestApiManager類(該類用來調用該服務的接口,並創建改進助洗劑)

public class RestApiManager { 

    private LoginApi loginApi; 

    public LoginApi login() { 
     if (loginApi==null) { 
      GsonBuilder gson=new GsonBuilder(); 
      gson.registerTypeAdapter(String.class, new StringDeserializer()); 
      Retrofit retrofit=new Retrofit.Builder() 
       .baseUrl("http://test:[email protected]:8080") 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 
      loginApi=retrofit.create(LoginApi.class); 
    } 
    return loginApi; 
} 

到RestApiManager

Call<User> callUser=restApiManager.login().login(loginData.getCompanyId(),loginData.getScreenName(),loginData.getPassword(),loginData.isActive()); 
callUser.enqueue(new Callback<User>() { 
    @Override 
    public void onResponse(Response<User> response, Retrofit retrofit) { 
     Log.d("Login","Login Response:"+response.body()); 
    } 

    @Override 
    public void onFailure(Throwable t) { 
     Log.d("Login","Login Response:"+t.getMessage()); 
    } 
}); 
的呼叫
+1

迴應是什麼?哪個回調被稱爲? – Blackbelt

+0

你應該放一個Loggin –

回答

1

跨平臺的會話管理在瀏覽器上不起作用。郵差是一個在瀏覽器平臺上運行的Web客戶端。

1.One溶液是保持在設備上的cookie。

檢查這個答案manage sessions with Android Application

2.其他的解決辦法是使用基於JSON網絡令牌身份驗證。

5

看起來也許你的要求應該有一個JSON的身體,而不是一個POST變量?您正在調用JSON webservice,並且您的示例參數看起來比POST更多的JSON。如果是這樣,那麼您可以在一個對象封裝的參數 -

public class User { 
    int companyId; 
    String screenName; 
    String password; 
    boolean active; 

    User(int companyId, String screenName, String password, boolean active) { 
     this.companyId = companyId; 
     this.screenName = screenName; 
     this.password = password; 
     this.active = active; 
} 

你的接口將是 -

public interface LoginApi {  
    @POST("/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name") 
    Call<User> login(@Body User user); 
} 

和構建您的來電爲 -

User user = new User(loginData.getCompanyId(),loginData.getScreenName(),loginData.getPassword(),loginData.isActive()); 
Call<User> callUser = restApiManager.login().login(user); 
1

我試着在我的本地PC上運行你的演示代碼,但沒有幸運,因爲你的服務器在你的局域網中,關於這個問題,如果你在服務器端使用cookie或會話授權,我會建議你嘗試setCookie處理程序如下,該PersistentCookieStore.java你可以找到here

private PersistentCookieStore cookieStore= new PersistentCookieStore(JApplication.getInstance().getApplicationContext()); 
CookieManager cookieManager = (new CookieManager(
      cookieStore, 
      CookiePolicy.ACCEPT_ALL)); 

okHttpClient.setCookieHandler(cookieManager); 
OkClient okClient = new OkClient(okHttpClient); 
RestAdapter restAdapter = new RestAdapter.Builder() 
      .setRequestInterceptor(new RequestInterceptor() { 
       @Override 
       public void intercept(RequestFacade request) { 
        request.addHeader(Constant.Header_UserAgent, getUserAgent()); 
       } 
      }) 
      .setClient(okClient) 
      .setEndpoint(URL) 
      .setErrorHandler(new HttpErrorHandler()) 
      .setConverter(new GsonConverter(gson)) 
      .build(); 
1

這可能是很難給我們幫您無需究竟哪些要求你改造API是構建知道。

因此,我建議您配置一個記錄攔截器,以便與真正發生的事情取得聯繫,如果您仍然不知道發生了什麼事情,可以通過更多信息向我們反饋。

檢查這個答案,瞭解如何與改造2配置LoggingInterceptor: Logging with Retrofit 2

希望它幫助。

此致敬禮。

0

我嘗試了這裏給出的所有解決方案,但不幸的是沒有任何工作。我發現從android內部調用lifer服務的唯一解決方案是使用lifer mobile sdk.It已經有方法和服務來調用liferay服務。您也可以致電您的定製服務。更多信息 Liferay Mobile SDK