2014-05-20 45 views
2

我的代碼改造返回錯誤401,空指針異常爲Android 4.4.2

private void autenticar(TipoLogin tipoLogin, Cidadao cidadao) throws Throwable { 
    // Captura o tipo de opção de login 
    final Integer opcao = tipoLogin.getCodigo(); 

    this.apiAcessoDAO = FabricaDAO.criarApiAcessoDAO(this.context); 


    Prefeitura prefeitura = null; 
    PrefeituraDAO prefeituraDAO = null; 

    try { 
     prefeituraDAO = FabricaDAO.criarPrefeituraDAO(context); 
     prefeitura = prefeituraDAO.carregar(); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); 

    // Crio um restadapter 
    RestAdapter restAdapter = new RestAdapter.Builder() 
      .setServer(prefeitura.getUrlBase()) 
      .setErrorHandler(new ExceptionNetwork(this.context)) 
      .setClient(new OkClient()) 
      .setConverter(new GsonConverter(gson)) 
      .build(); 

    CidadaoAPI cidadaoAPI = restAdapter.create(CidadaoAPI.class); 

    ApiAcesso apiAcesso = null; 

    // opção selecionada pelo usuário (tipo login) 
    switch (opcao) 
    { 
     case 1: 
      apiAcesso = cidadaoAPI.autenticar(cidadao.getSenhaPortal(), tipoLogin.getUrl(), cidadao.getFoneCelular1()); 
      break; 
     case 2: 
      apiAcesso = cidadaoAPI.autenticar(cidadao.getSenhaPortal(), tipoLogin.getUrl(), cidadao.getEmail()); 
      break; 
     case 3: 
      apiAcesso = cidadaoAPI.autenticar(cidadao.getSenhaPortal(), tipoLogin.getUrl(), cidadao.getCpf()); 
      break; 
     case 4: 
      apiAcesso = cidadaoAPI.autenticar(cidadao.getSenhaPortal(), tipoLogin.getUrl(), cidadao.getNumeroCartaoSus()); 
      break; 
     case 5: 
      apiAcesso = cidadaoAPI.autenticar(cidadao.getSenhaPortal(), tipoLogin.getUrl(), cidadao.getRegistroCidadao()); 
      break; 
    } 

    // Insere no banco de dados 
    try { 
     this.apiAcessoDAO.deletar(); 
     this.apiAcessoDAO.inserir(apiAcesso); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 

我的錯誤處理

public class ExceptionNetwork implements ErrorHandler { 

private Context context; 

public ExceptionNetwork(Context context) { 
    this.context = context; 
} 

/** 
* Return a custom exception to be thrown for a {@link retrofit.RetrofitError}. It is recommended that you 
* pass the supplied error as the cause to any new exceptions. 
* <p/> 
* If the return exception is checked it must be declared to be thrown on the interface method. 
* <p/> 
* Example usage: 
* <pre> 
* class MyErrorHandler implements ErrorHandler { 
* &#64;Override public Throwable handleError(RetrofitError cause) { 
*  Response r = cause.getResponse(); 
*  if (r != null &amp;&amp; r.getStatus() == 401) { 
*  return new UnauthorizedException(cause); 
*  } 
*  return cause; 
* } 
* } 
* </pre> 
* 
* @param cause the original {@link retrofit.RetrofitError} exception 
* @return Throwable an exception which will be thrown from a synchronous interface method or 
* passed to an asynchronous error callback. Must not be {@code null}. 
*/ 
@Override 
public Throwable handleError(RetrofitError cause) { 

    Response response = cause.getResponse(); 

    if(response.getStatus() == 401) { 
     try { 
      return new Exception(this.context.getString(R.string.internete_nao_autorizado)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    else 
    if(response != null && response.getStatus() == 404) { 
     try { 
      return new Exception(this.context.getString(R.string.internete_nao_encontrado)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    else 
    if(response != null && response.getStatus() == 307) 
    { 
     try 
     { 
      return new Exception(this.context.getString(R.string.internete_link_redirecionado)); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    else 
    if(response == null) 
    { 
     try { 
      return new Exception(this.context.getString(R.string.internete_link_offiline)); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    return new Exception(this.context.getString(R.string.internete_link_offiline)); 
} 

}

錯誤

05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ java.lang.NullPointerException 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at br.com.appsap.app.exeption.ExceptionNetwork.handleError(ExceptionNetwork.java:52) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:286) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at $Proxy2.autenticar(Native Method) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at br.com.appsap.app.controller.implementacao.CidadaoControllerImplementacao.autenticar(CidadaoControllerImplementacao.java:176) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at br.com.appsap.app.controller.implementacao.CidadaoControllerImplementacao.login(CidadaoControllerImplementacao.java:129) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at br.com.appsap.app.view.LoginActivity$PlaceholderFragment.onClick(LoginActivity.java:320) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at android.view.View.performClick(View.java:4438) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at android.os.Looper.loop(Looper.java:136) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
05-20 13:27:21.968 1243-1243/br.com.appsap.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method) 
+0

如果你打算在檢查空指針的if語句,那麼你應該確保你是在第一次如果檢查空指針在處理錯誤內的條件也是如此。打印出來的行號與我所知道的行不匹配,但如果你沒有得到迴應,這可能是罪魁禍首。 –

+0

我不知道爲什麼空指針來了。當我強制錯誤401. – touche

+0

https://github.com/square/retrofit/issues/382 retrofitError getResponse返回null爲401 –

回答

1

解決你的問題你需要包含ok-http-lib郭寶宏。但要注意兼容性的issues.Here是我的工作設置:

compile 'com.squareup.retrofit:retrofit:1.7.1' 
compile 'com.squareup.okhttp:okhttp:2.0.0' 
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' 
+0

我把頁面請求登錄和密碼授權,並解決了問題,謝謝。 – touche