public static intuploadImg(finalString mimeType, finalInputStream inputStream, finalUploadListener listener,BoxFile mFile,String path, intaccountId,String name,
String id){
MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
OkHttpClient client =newOkHttpClient();
JSONObject json =newJSONObject();
try{
json.put("name",name);
JSONObject parentIdObject =newJSONObject();
parentIdObject.put("id",id);
json.put("parent",parentIdObject);
}catch(JSONException e) {
e.printStackTrace();
}
MultipartBody.Builder builder =newMultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("attributes",json.toString())
.addFormDataPart("file","@"+ name,RequestBody.create(MEDIA_TYPE_PNG, newFile(path)));
RequestBody fileBody =newRequestBody() {
@Override
publicMediaTypecontentType() {
returnMediaType.parse(mimeType);
}
@Override
public voidwriteTo(BufferedSink bufferedSink)throwsIOException {
intcount;
intbufferSize =20480;
byte[] buffer =new byte[bufferSize];
do{
count =inputStream.read(buffer,0,bufferSize);
if(count != -1) {
bufferedSink.write(buffer,0,count);
Log.e("RequestBody","count="+ count);
if(listener!=null) {
listener.onProgress(count);
}
}
}while(count != -1);
}
};
builder.addPart(fileBody);
Request request =newRequest.Builder()
.header("Authorization","Bearer "+tokenMap.get(accountId).key)
.url("https://upload.box.com/api/2.0/files/content")
.post(builder.build())
.build();
Response response =null;
try{
response = client.newCall(request).execute();
}catch(IOException e) {
e.printStackTrace();
}
if(response !=null&& response.isSuccessful()) {
Log.e("abc","上传成功");
JSONObject jsonObject = NetWorkUtils.convertJson(response.body().byteStream());
JSONArray jsonArray = jsonObject.optJSONArray("entries");
for(inti=0;i
JSONObject json1 = jsonArray.optJSONObject(i);
BoxFile boxFile =newBoxFile();
fillBoxFile(json1,boxFile,mFile.getPath(),mFile.getFileId(),mFile.getAccountId());
fileHelper.saveOrUpdate(boxFile);
}
returnResultConstant.SUCCESS;
}else{
Log.e("abc","上传失败====="+ response.toString());
returnResultConstant.FAILED;
}
}