我正在尝试使用Robospice with Retrofit将图像上传到我的Google appengine blobstore.我可以获得GAE提供的上传URL,但是当我尝试将带有图像的URL作为Multipart POST发送时,我得到一个例外:
E // RequestRunner.java:134(24689):Thread-3363请求网络执行期间发生异常:null
E // RequestRunner.java:134(24689):retrofit.RetrofitError
E // RequestRunner.java:134(24689):at retrofit.RestAdapter $ RestHandler.invokeRequest(RestAdapter.java:343)
E // RequestRunner.java:134(24689):at retrofit.RestAdapter $ RestHandler.invoke(RestAdapter.java:222)
E // RequestRunner.java:134(24689):at $ Proxy0.uploadImage(Native Method)
E // RequestRunner.java:134(24689):在ginger.connexus.network.UploadImage.loadDataFromNetwork(UploadImage.java:24)
E // RequestRunner.java:134(24689):在ginger.connexus.network.UploadImage.loadDataFromNetwork(UploadImage.java:1)
E // RequestRunner.java:134(24689):at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
E // RequestRunner.java:134(24689):at com.octo.android.robospice.request.RequestRunner.processRequest(RequestRunner.java:130)
E // RequestRunner.java:134(24689):at com.octo.android.robospice.request.RequestRunner $ 1.run(RequestRunner.java:197)
E // RequestRunner.java:134(24689):at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:390)
E // RequestRunner.java:134(24689):at java.util.concurrent.FutureTask.run(FutureTask.java:234)
E // RequestRunner.java:134(24689):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
E // RequestRunner.java:134(24689):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)
E // RequestRunner.java:134(24689):at java.lang.Thread.run(Thread.java:841)
D // RequestProgressManager.java:75(24689):发送进度完成
我的API界面
public interface …Run Code Online (Sandbox Code Playgroud) google-app-engine android image-uploading robospice retrofit
我正在尝试附加SpiceManager服务,但我得到了:
java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.octo.android.robospice.SpiceService$SpiceServiceBinder
at com.octo.android.robospice.SpiceManager$SpiceServiceConnection.onServiceConnected(SpiceManager.java:1072)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
服务流程看起来有点像这样:
class MyService extends Service {
// ...
SpiceManager mSpiceManager = new SpiceManager(UncachedSpiceService.class);
// ...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mSpiceManager.start(this);
mSpiceManager.execute(new MyRequest(), new RequestListener<String>() {
// Handle of request
})
// ...
return START_STICKY;
}
// …Run Code Online (Sandbox Code Playgroud) 我一直在关注这篇博客文章,其中展示了如何使用Mockito和Retrofit模拟请求.问题是我在Robospice上使用它们,它不需要Callback在服务接口上提供as参数(因为它将是一个同步调用):
@GET("/foo/bar")
User foo(@Query("bar") String baz);
Run Code Online (Sandbox Code Playgroud)
所以我不能以这种方式拦截我测试的回调:
Mockito.verify(mockApi).repositories(Mockito.anyString(), cb.capture());
User user = new User();
cb.getValue().success(user, null);
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?谢谢!
我正在使用Retrofit/Robospice在我构建的应用程序中使用RetrofitGsonSpiceService进行api调用.使用GSON转换器将所有响应转换为POJO,但是我需要从响应头中检索一些信息.我找不到任何获取标题的方法(如果请求不成功,我只能获取标题,因为原始响应是在错误对象中发送的!)如何在转换之前拦截响应以获取标题?
我想知道是否需要SpiceManager的shouldStop()方法.我认为它应该从活动/片段中取消绑定可能的侦听器,但如果活动将被杀死,它是否重要?垃圾收集器仍将清理活动/片段,后台服务仍将单独完成.或者我错了吗?当你在后台线程中忘记一些引用时,Android真的会泄漏吗?
源代码中有一些(错误的)注释:对于shouldStop(),它表示它将异步进行解绑定,并且对于shouldStopandJoin()同步执行.但是应该停止调用toStopAndJoin,而且应该停止注释并且注释它主要是一种测试方法.那么我们真的需要在activity的onStop()回调中调用shouldStop吗?
我问这个是因为我想在Application上下文中有一个spicemanager实例,它没有处理清理或停止事务的特殊方法.所以我想确保没有泄漏或类似的东西.要清楚,不是真的在应用程序上下文中,而是从应用程序上下文启动并管理所有后台内容(如robospice请求或位置内容)的自定义"控制器".我正在尝试模拟MVC模式,其中所有逻辑实际上都独立于仅具有用于更改UI的@Subscribe方法的活动和片段.
编辑:实际上,如果我从应用程序上下文中调用它,那么它与Activity或Fragment的情况不同.即使活动或碎片确实泄漏,应用程序也不应该,对吧?
谢谢你的评论.
几个小时以来我一直在研究以下问题,但还没有找到解决问题的方法.我已经尝试了Stack Overflow中的以下修复(Android Studio更新到1.0会破坏Gradle插件v0.13.1之后的MultiDex和重复Zip条目),但它们都没有工作.
我在尝试构建程序时遇到以下错误:
Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: org/apache/commons/io/CopyUtils.class
Run Code Online (Sandbox Code Playgroud)
该错误似乎表明commons-io在构建过程中被包含两次
我正在使用Android Studio和Gradle来包含多个Robospice依赖项.这是我的Gradle构建文件的依赖项部分:
dependencies {
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile'com.google.api-client:google-api-client-android:1.19.0'
// You must install or update the Google Repository through the SDK manager to use this dependency.
// The Google Repository (separate from the corresponding library) can be found in the Extras category.
//compile 'com.google.android.gms:play-services:4.3.23'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.jakewharton:butterknife:${butterknifeVersion}"
compile 'com.sun.jersey:jersey-bundle:1.8'
compile 'com.google.code.gson:gson:2.3'
compile 'org.codehaus.jackson:jackson-core-asl:1.9.0' …Run Code Online (Sandbox Code Playgroud) 我正在尝试阅读JSON数组,其格式如下:[{"vehicle_id":"76","color":"red"},{"vehicle_id":"7","color":"blue"}]遵循Robospice的入门指南.
Vehicle.java
public class Vehicle {
@JsonProperty("vehicle_id")
private int vehicleID;
@JsonProperty("color")
private String color;
}
Run Code Online (Sandbox Code Playgroud)
(塞特犬和吸气者跟随)
给出错误的类:VehiclesRequest.class
public class VehiclesRequest extends SpringAndroidSpiceRequest<Vehicle> {
private static final String METHOD = "systemVehicles";
public SystemVehiclesRequest() {
super(Vehicle.class);
}
@Override
public Vehicle[] loadDataFromNetwork() throws Exception {
return getRestTemplate().getForObject(
FULL_URL,
Vehicle[].class);
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我重写了loadDataFromNetwork()方法,然后将其传递给spiceManager.execute()我的活动中的方法,并使用自定义侦听器从请求中读取数据.但是我无法返回和阵列(我也尝试List<>过)loadDataFromNetwork(),这里最好的解决方法是什么?我知道我可以用不同的方式获取数据,但我仍然希望使用我的监听器并能够以try这种方式进行阻塞.
我的应用程序需要定期向服务器请求新数据.我已经研究过这个,很多人建议使用同步适配器与服务器同步,但是我的要求发生了变化,我需要做以下过程.是否仍然建议使用同步适配器,或者我可以使用任何其他库来有效地生成以下Http请求序列.
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
ZipFile imageZipFile;
/*this is a http post request and the size the zipfile is around 1Mb*/
ZipFile resultFile=makeHttpPostRequest(String URL, String payload);
SomeObject result= processZipFile(resultZipFile);
saveData(result);
for(String url:result.getUrls()){
/* this is a HttpGet request which returns a zip file that
contains 8 images , total size of zip would be around 200kb*/
imageZipFile= makeHttpGetRequest(url);
saveImageZipToDisk(imageZipFile)
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在发出Http Post请求以获取包含图像URL的一些数据,并使用这些URL来生成新的HttpGet请求.我需要POST的结果以及我的应用程序运行的图像.
这是使用同步适配器的有效方法还是这是完全不可接受的?或者我可以使用Volley/Robo spice将图像请求生成到多个线程吗?对不起,我是新手,但这是我一直在努力解决的问题.
更新:
因此,在回顾了Volley和Robospice的优点和缺点之后,我正在使用凌空,因为我可以自定义代码并对缓存机制有更多的控制权.
Logcat错误
07-30 06:43:48.135: E//DefaultRequestRunner.java:153(29678): 06:43:48.147 Thread-9164 An exception occurred during request network execution :Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token
07-30 06:43:48.135: E//DefaultRequestRunner.java:153(29678): at [Source: java.util.zip.GZIPInputStream@420e4cc8; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
07-30 06:43:48.135: E//DefaultRequestRunner.java:153(29678): at [Source: java.util.zip.GZIPInputStream@420e4cc8; line: 1, column: 1]
07-30 06:43:48.135: E//DefaultRequestRunner.java:153(29678): org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token
07-30 …Run Code Online (Sandbox Code Playgroud) 我有一个Android应用程序使用Robospice与Jackson + Spring来执行REST请求.这是有效的,除了Robospice似乎没有缓存响应.我还确保缓存密钥在请求之间保持一致.
我正在设置像这样的SpiceManager:
private SpiceManager mRestManager = new SpiceManager(JacksonSpringAndroidSpiceService.class);
Run Code Online (Sandbox Code Playgroud)
然后,我正在执行这样的请求:
mRestManager.execute(customSpiceRequest, requestCacheKey,
DurationInMillis.ALWAYS, new CustomRequestListener())
Run Code Online (Sandbox Code Playgroud)
我不知道它是否相关,但我的customSpiceRequest扩展了SpringAndroidSpiceRequest.我已经确定requestCacheKey在请求之间也是相同的.
我还需要做些什么才能在请求之间启用缓存吗?
我正在使用RoboSpice进行Retrofit.我正在调用REST调用,但是一个调用是一个问题,因为从不调用响应侦听器 - onRequestFailure()和onRequestSuccess()
...用户选择UI选项后
CreateCustomerRequest createCustomerRequest = new CreateCustomerRequest("1001", "John", "Smith");
this.getSpiceManager().execute(createCustomerRequest, new CreateCustomerRequestListener());
private class CreateCustomerRequestListener implements com.octo.android.robospice.request.listener.RequestListener<ErrorResponse> {
@Override
public void onRequestFailure(SpiceException spiceException) {
// Never called
}
@Override
public void onRequestSuccess(ErrorResponse errorResponse) {
// Never called
}
}
Run Code Online (Sandbox Code Playgroud)
public class CreateCustomerRequest extends RetrofitSpiceRequest<ErrorResponse, EmbarApi> {
private String id;
private String firstName;
private String lastName;
public CreateCustomerRequest(String id, String firstName, String lastName) {
super(ErrorResponse.class, EmbarApi.class);
this.id = id;
this.firstName = firstName;
this.lastName= lastName;
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我是Robospice /改造图书馆的新手.我从github得到了一些样本.https://github.com/octo-online/RoboSpice-samples/tree/release/robospice-sample-retrofit.
我的理解:
请求是"githubRequest",响应是"Contributor.List".web服务由getSpiceManager().execute执行.
代码片段:
@Override
protected void onStart() {
super.onStart();
getSpiceManager().execute(githubRequest, "github", DurationInMillis.ONE_MINUTE, new ListContributorRequestListener());
}
public final class ListContributorRequestListener implements RequestListener<Contributor.List> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Toast.makeText(SampleSpiceActivity.this, "failure", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestSuccess(final Contributor.List result) {
Toast.makeText(SampleSpiceActivity.this, "success", Toast.LENGTH_SHORT).show();
updateContributors(result);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:我想从应用程序检查请求/响应("githubRequest"/"Contributor.List")是否将正确的JSON发送到服务.那么如何sysout JSON请求和响应.但请求/响应是POJO对象.但是如果我想打印JSON请求和响应,我该怎么做?有人帮我这样做吗?
通过异步执行一堆请求的最简单方法是RoboSpice什么?
我在某处读到了我需要实现的RequestRunner但是我不知道将它合并的方式SpiceManager,任何想法?