4

我正在尝试使用带有 Retrofit 的 Robospice 将图像上传到我的 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): 在改造.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:343)

E//RequestRunner.java:134(24689): 在改造.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): 在 com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)

E//RequestRunner.java:134(24689): 在 com.octo.android.robospice.request.RequestRunner.processRequest(RequestRunner.java:130)

E//RequestRunner.java:134(24689): 在 com.octo.android.robospice.request.RequestRunner$1.run(RequestRunner.java:197)

E//RequestRunner.java:134(24689): 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)

E//RequestRunner.java:134(24689): 在 java.util.concurrent.FutureTask.run(FutureTask.java:234)

E//RequestRunner.java:134(24689): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)

E//RequestRunner.java:134(24689): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

E//RequestRunner.java:134(24689): 在 java.lang.Thread.run(Thread.java:841)

D//RequestProgressManager.java:75(24689):发送进度完成

我的 API 接口

public interface MyApi {
    @Multipart
    @POST("/{uploadurl}")
    MyImage uploadImage(
            @Path("uploadurl") String uploadurl,
            @Part("stream") long streamId,
            @Part("image") TypedFile image);
}

我的改造 Gson Spice 服务

public class MyService extends RetrofitGsonSpiceService {

    private final static String BASE_URL = "theinternet";

    @Override
    protected String getServerUrl() {
        return BASE_URL;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        addRetrofitInterface(MyApi.class);
    }

}

我的改装香料请求课程

public class UploadImage extends RetrofitSpiceRequest<MyImage, MyApi> {

    private final String uploadurl;
    private final long streamId;
    private final TypedFile image;

    public UploadImage(String uploadurl, long streamId, File image) {
            super(MyImage.class, MyApi.class);
            this.uploadurl = uploadurl;
            this.streamId = streamId;
            this.image = new TypedFile("image/jpeg", image);
    }

    @Override
    public MyImage loadDataFromNetwork() throws Exception {
        return getService().uploadImage(uploadurl, streamId, image);
    }
}

我的执行电话

UploadImage upload = new UploadImage(uploadUrl, streamId, file);
getSpiceManager().execute(upload, new MyImageUploadListener());
4

1 回答 1

9

尝试使用@EncodedPath而不是@Path自己进行 URL 编码。

public interface MyApi {

    @Multipart
    @POST("/{uploadurl}")
    MyImage uploadImage(@EncodedPath("uploadurl") String uploadUrlEncoded,
            ... );
}

为了处理“%2F”问题,我将使用下一个片段

String encodedValue = URLEncoder.encode(String.valueOf(uploadUrl), "UTF-8");
// URLEncoder encodes for use as a query parameter. Path encoding uses %20 to
// encode spaces rather than +. Query encoding difference specified in HTML spec.
// Any remaining plus signs represent spaces as already URLEncoded.
encodedValue = encodedValue.replace("+", "%20");
String uploadUrlEncoded = encodedValue.replace("%2F", "/");
于 2013-10-11T14:15:57.993 回答