错误文件创建失败 - 上传JSON

时间:2013-10-14 11:24:44

标签: java android json upload httpclient

您好我试图将文件上传到服务器。它的完成方式是这样的: 1.通过调用JSON URL获取上传URL 2.上传到uploadUrl 3.得到回应

关于api电话的文件:

/file/uploadUrl

{ error: "", uploadUrl: <string>, progressUrl: <string> }

The returned URL is valid for a limited amount of time, you need to request a new one for each upload.
Issue a POST request to the "uploadUrl" to upload the file. You can poll "progressUrl" to fetch the current progress.
The POST request needs to be multipart/form-data encoded and contain a "file" field.
cUrl example: curl -F "file=@somefile.rar" "http://s6.baycdn.com/upload/93b37e57/4e691e0c/6/0?X-Progress-ID=4e639fcd34CC2F73796CF81679C605643A8F99CF"
Note: in order to upload a file to your account, you need to first log in using /account/login and then append ?session=XYZ to /file/uploadUrl
On success, the "uploadUrl" will respond with an object:
{ error: "", fileId: <string>, size: <int>, sha1: <string>, infoToken: <string>, deleteToken: <string>, linksUrl: <string>, downloadUrl: <string>, deleteUrl: <string>, }

我的代码:( onClick)

case R.id.upload:
            asyncTask = new JsonAsync();
            asyncTask.setJsonListener(new JsonListener() {
                public void onObjectReturn(JSONObject object) {
                    handleJsonObject(object);
                }
            });
            asyncTask.execute("http://api.bayfiles.net/v1/file/uploadUrl?session=" + sessionId);
            return true;

handleJsonObject:

 private void handleJsonObject(JSONObject object) {

        try {
            sUpload = object.getString("uploadUrl");            
            HttpClient httpclient = new DefaultHttpClient();

            //post request to send the video 

            File sdCardRoot = Environment.getExternalStorageDirectory();
            File myDir = new File(sdCardRoot, "Download");

            HttpPost httppost = new HttpPost(sUpload);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy( policy);
            FileBody video_file1 = new FileBody(new File(myDir + "/test.txt"));
            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("file=@test.txt", video_file1);                    
            httppost.setEntity(reqEntity);

            // DEBUG
            System.out.println( "executing request " + httppost.getRequestLine( ) );
            HttpResponse response = null;
            try {
                response = httpclient.execute( httppost );
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            HttpEntity resEntity = response.getEntity( );

            // DEBUG
            System.out.println( response.getStatusLine( ) );
            if (resEntity != null) {
              try {
                System.out.println( EntityUtils.toString( resEntity ) );
            } catch (org.apache.http.ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            } // end if
            if (resEntity != null) {
              try {
                resEntity.consumeContent( );
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            } // end if

            httpclient.getConnectionManager( ).shutdown( );


        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data: " + e.toString());
            Crouton.makeText(this, "Something went wrong!", Style.ALERT).show();
        }
    }

我从服务器(JSON)得到的错误是:{“error”:“file creation failes”}

和logcat:

10-14 13:17:29.954: I/System.out(22705): executing request POST http://s5.baycdn.com/upload/199808fb/52615115/7/2084659?X-Progress-ID=525bd2d5566FC893A0505FABB493A4215C7EC3AF HTTP/1.1
10-14 13:17:31.125: I/System.out(22705): HTTP/1.1 200 OK
10-14 13:17:31.125: I/System.out(22705): {"error":"file creation failed"}

0 个答案:

没有答案