我使用RetroFit版本1.9.0发出PUT请求,将图像添加到某个位置,同时也使用访问cookie,但在图像传输开始之前,我收到错误:
09-14 13:35:12.718 21896-22689/com.company.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: retrofit.RetrofitError: com.squareup.okhttp.Request$Builder
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy10.addPicture(Native Method)
at com.company.app.parser.ImageSender$BackgroundTask.doInBackground(ImageSender.java:70)
at com.company.app.parser.ImageSender$BackgroundTask.doInBackground(ImageSender.java:39)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NoClassDefFoundError: com.squareup.okhttp.Request$Builder
at retrofit.client.OkClient.createRequest(OkClient.java:57)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy10.addPicture(Native Method)
at com.company.app.parser.ImageSender$BackgroundTask.doInBackground(ImageSender.java:70)
at com.company.app.parser.ImageSender$BackgroundTask.doInBackground(ImageSender.java:39)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
请求的方式如下:
private class BackgroundTask extends AsyncTask<TypedFile, Void, JSONObject> {
RestAdapter restAdapter;
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestInterceptor.RequestFacade request) {
request.addHeader("Cookie", CookieJar.openJar().getCookie());
}
};
@Override
protected void onPreExecute() {
restAdapter = new RestAdapter.Builder()
.setEndpoint(BasicData.BASE_URL)
.setRequestInterceptor(requestInterceptor)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
}
@Override
protected JSONObject doInBackground(TypedFile... params) {
IApiMethods methods = restAdapter.create(IApiMethods.class);
JSONObject response = methods.addPicture(CardManager.getInstance().getCurrentCard().getId(), params[0]);
return response;
}
@Override
protected void onPostExecute(JSONObject response) {
if (response != null) {
parserReceiver.parseSuccess(null, parserName);
}
}
}
东西
public interface IApiMethods {
@Headers({
"Connection: Keep-Alive"
})
@Multipart
@PUT("/0.1/ideas/{id}/picture")
JSONObject addPicture(@Path("id") String ideaId, @Part("file") TypedFile picture);
}
答案 0 :(得分:3)
我认为Retrofit可能需要OkHttp依赖
compile 'com.squareup.okhttp:okhttp:2.2.0'