如何使用Android中的Retrofit将多个文件发布到服务器?

时间:2015-03-10 07:56:35

标签: android post server retrofit

我正在尝试使用Retrofit发布多个文件但总是得到 错误

failure  null 

在活动代码中:

MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();

multipartTypedOutput.addPart("test_image",
                                new TypedFile("image/png",new File("/sdcard/test.png")));
multipartTypedOutput.addPart("test_audio",
                                new TypedFile("audio/mp3",new File("/sdcard/testaudio.mp3")));



RestAdapter restAdapter = new RestAdapter.Builder()
                 .setEndpoint("http://192.168.10.115/test.php")
                 .setClient(new OkClient(getClient()))
                 .build();

 GetData apiService =restAdapter.create(GetData.class);
 apiService.createPostWithAttachments(multipartTypedOutput, new Callback<Response>() {

                        @Override
                        public void failure(RetrofitError retrofitError) {
                            Log.v("", "failure "+retrofitError.getMessage());

                        }

                        @Override
                        public void success(Response s, Response response) {
                            BufferedReader reader = null;
                            StringBuilder sb = new StringBuilder();
                            try {

                                reader = new BufferedReader(new InputStreamReader(s.getBody().in()));

                                String line;

                                try {
                                    while ((line = reader.readLine()) != null) {
                                        sb.append(line);
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            String result = sb.toString();
                            Log.v("", "success "+result.toString());
                        }
                    });

OkClient是:

private OkHttpClient getClient() {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(5, TimeUnit.MINUTES);
        client.setReadTimeout(5, TimeUnit.MINUTES);
        return client;
    }

界面是:

public interface GetData {
    @POST("/")
    void createPostWithAttachments(@Body MultipartTypedOutput attachments,Callback<Response> response);
}

我使用了以下的库:

okhttp-2.2.0.jar
okhttp-urlconnection-2.0.0.jar
okio-1.2.0.jar
retrofit-1.9.0.jar

PHP服务器代码:

$target_dir = "/images/";
    $target_dir = $target_dir . basename($_FILES["test_image"]["name"]);

    $atarget_dir = $target_dir . basename($_FILES["test_audio"]["name"]);
    if(move_uploaded_file($_FILES["test_image"]["tmp_name"], $target_dir))
      {
         $msg = "The file ". basename($result[0]). " has been uploaded.";
         $send_arr['success'] = 1;
         $send_arr['message'] = $msg;
         echo json_encode($send_arr);
      }
      else if(move_uploaded_file($_FILES["test_audio"]["tmp_name"], $atarget_dir))
      {
         $msg = "The file ". basename($result[0]). " has been uploaded.";
         $send_arr['success'] = 1;
         $send_arr['message'] = $msg;
         echo json_encode($send_arr);
      }
    else 
      {
         $msg = "Sorry, there was an error uploading your file.";
         $send_arr['success'] = 0;
         $send_arr['message'] = $msg;
         echo json_encode($send_arr);
      }

提供错误:

03-14 13:15:46.137: W/System.err(11080): retrofit.RetrofitError: timeout
03-14 13:15:46.137: W/System.err(11080):    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
03-14 13:15:46.137: W/System.err(11080):    at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
03-14 13:15:46.137: W/System.err(11080):    at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
03-14 13:15:46.137: W/System.err(11080):    at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
03-14 13:15:46.137: W/System.err(11080):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-14 13:15:46.137: W/System.err(11080):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-14 13:15:46.137: W/System.err(11080):    at retrofit.Platform$Android$2$1.run(Platform.java:142)
03-14 13:15:46.137: W/System.err(11080):    at java.lang.Thread.run(Thread.java:841)
03-14 13:15:46.147: W/System.err(11080): Caused by: java.io.InterruptedIOException: timeout
03-14 13:15:46.147: W/System.err(11080):    at okio.AsyncTimeout.exit(AsyncTimeout.java:258)
03-14 13:15:46.147: W/System.err(11080):    at okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
03-14 13:15:46.147: W/System.err(11080):    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:255)
03-14 13:15:46.147: W/System.err(11080):    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:249)
03-14 13:15:46.147: W/System.err(11080):    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:791)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:90)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:784)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:645)
03-14 13:15:46.147: W/System.err(11080):    at com.squareup.okhttp.Call.getResponse(Call.java:263)
03-14 13:15:46.157: W/System.err(11080):    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:219)
03-14 13:15:46.157: W/System.err(11080):    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:192)
03-14 13:15:46.157: W/System.err(11080):    at com.squareup.okhttp.Call.execute(Call.java:79)
03-14 13:15:46.157: W/System.err(11080):    at retrofit.client.OkClient.execute(OkClient.java:53)
03-14 13:15:46.157: W/System.err(11080):    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
03-14 13:15:46.157: W/System.err(11080):    ... 7 more
03-14 13:15:46.157: W/System.err(11080): Caused by: java.net.SocketException: Socket closed
03-14 13:15:46.157: W/System.err(11080):    at libcore.io.Posix.recvfromBytes(Native Method)
03-14 13:15:46.157: W/System.err(11080):    at libcore.io.Posix.recvfrom(Posix.java:141)
03-14 13:15:46.157: W/System.err(11080):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
03-14 13:15:46.157: W/System.err(11080):    at libcore.io.IoBridge.recvfrom(IoBridge.java:515)
03-14 13:15:46.167: W/System.err(11080):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:489)
03-14 13:15:46.167: W/System.err(11080):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-14 13:15:46.167: W/System.err(11080):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
03-14 13:15:46.167: W/System.err(11080):    at okio.Okio$2.read(Okio.java:136)
03-14 13:15:46.167: W/System.err(11080):    at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
03-14 13:15:46.167: W/System.err(11080):    ... 22 more

当我尝试发布多张图片时,请始终获得回复nullTimeout-exceptionfailure请告诉我如何使用Retrofit发布多张图片?< / p>

谢谢, 吉里什

0 个答案:

没有答案