我的代码
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 {
* @Override public Throwable handleError(RetrofitError cause) {
* Response r = cause.getResponse();
* if (r != null && 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 :(得分:1)
要解决您的问题,您需要包含ok-http-library。但请注意兼容性问题。这是我的工作设置:
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'