我一直在开发 Android 应用程序,我正在使用 Retrofit。
如何处理 onFailure(Throwable t) 回调 NoInternetConnection 和 OtherError
我检查了一些关于 stackoverflow 的问题,但没有帮助,因为我正在使用 retrofit 2
compile 'com.squareup.retrofit2:retrofit:2.1.0'
回调代码
public class AvsCallBack<T> implements Callback<T> {
private static final String TAG = "AvsCallBack";
private AvsCallbackInterface<T> avsInterface;
private Activity activity;
private boolean validateError = true;
public AvsCallBack(Activity activity, AvsCallbackInterface<T> avsInterface) {
this.activity = activity;
this.avsInterface = avsInterface;
}
public AvsCallBack(Activity activity, AvsCallbackInterface<T> avsInterface, boolean validateError) {
this.activity = activity;
this.avsInterface = avsInterface;
this.validateError = validateError;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (response.isSuccessful()) {
if (BuildConfig.DEBUG) Log.d(TAG, new Gson().toJson(response.body()));
avsInterface.onSuccess(call, response.body());
} else {
onFailure(call, null);
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
if (validateError) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Retrofit Exception -> " + ((t != null && t.getMessage() != null) ? t.getMessage() : "---"));
if (t != null && (t instanceof IOException || t instanceof SocketTimeoutException || t instanceof ConnectException)) {
if (t instanceof SocketTimeoutException || t instanceof TimeoutException) {
((BaseActivity) activity).showToast("Oops something went wrong");
//avsInterface.onError(call, new AvsException("Oops something went wrong, please try again later..."));
} else {
((BaseActivity) activity).showToast("Please check your internet connection...");
//avsInterface.onError(call, new AvsException("Please check your internet connection..."));
}
} else {
((BaseActivity) activity).showToast("Oops something went wrong");
}
if (BuildConfig.DEBUG)
Log.d(TAG, "Avs Exception -> " + ((t != null && t.getMessage() != null) ? t.getMessage() : "---"));
}
avsInterface.onError(call, t);
}
}
我的界面
public interface AvsCallbackInterface<T> {
void onSuccess(Call<T> call, T t);
void onError(Call<T> call, Throwable throwable);
}
最佳答案
引自 here :
When
Throwableis passed to the failure, the callback is anIOException, this means that it was a network problem (socket timeout, unknown host, etc.). Any other exception means something broke either in serializing/deserializing the data or it's a configuration problem.You can do
t instanceof IOExceptionto determine network problem and react appropriately.A 401 (or any non-2xx response code) will actually go to the response callback, because it was a successful response even though it may not have been a successful operation on the server. You can check this in
onResponseby callingresponse.isSuccess().
关于Android 改造处理 onFailure() 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041182/
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby-vips的github页面上的链接,我们将不胜感激!如果有ruby-
我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d
我正在尝试解析网页,但有时会收到404错误。这是我用来获取网页的代码:result=Net::HTTP::getURI.parse(URI.escape(url))如何测试result是否为404错误代码? 最佳答案 像这样重写你的代码:uri=URI.parse(url)result=Net::HTTP.start(uri.host,uri.port){|http|http.get(uri.path)}putsresult.codeputsresult.body这将打印状态码和正文。
我查看了Stripedocumentationonerrors,但我仍然无法正确处理/重定向这些错误。基本上无论发生什么,我都希望他们返回到edit操作(通过edit_profile_path)并向他们显示一条消息(无论成功与否)。我在edit操作上有一个表单,它可以POST到update操作。使用有效的信用卡可以正常工作(费用在Stripe仪表板中)。我正在使用Stripe.js。classExtrasController5000,#amountincents:currency=>"usd",:card=>token,:description=>current_user.email)
当profile为nil时,总是让我感到悲伤...我该怎么办? 最佳答案 在View中使用变量之前,始终检查变量是否为nil。我确信这个问题有更优雅的解决方案,但这应该能让您入门。 关于ruby-on-rails-Rails处理.Erb与Nils,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/2709605/
我有一个应用程序专门使用Facebook作为身份验证提供程序,并正确设置了生产模式的回调。为了让它工作,您需要为您的Facebook应用程序提供一个站点URL和一个用于回调的站点域,在我的例子中是http://appname.heroku.com和appname。heroku.com分别。问题是我的Controller设置为只允许经过身份验证的session,所以我无法在开发模式下查看我的应用程序,因为Facebook应用程序的域显然没有设置为本地主机。如何在不更改Facebook设置的情况下解决这个问题? 最佳答案 创建另一个域l