public void postAction() { onEvent(this, AnaticsManager.EVENT.CLICK, "postAction"); MultipartBuilder builder = new MultipartBuilder(); builder.type(MultipartBuilder.FORM); builder.addFormDataPart("Score", "33"); builder.addFormDataPart("PlayerName", "wqr"); HttpRequester.post( new RequestCallBack() { @Override public void _onStart() { textView.append("onStart...\n"); LoadViewManager.addLargeView(self()); } @Override protected void _onFinish() { textView.append("onFinish...\n"); LoadViewManager.hideLargeView(); } @Override public void _onFailure(final IOException e) { textView.append(Html.fromHtml(String.format("Throwable=%s", e) + "\n")); } @Override public void _onResponse(String response) { textView.append(response + "\n"); } }, URL, builder.build()); }
// post with file public static void asyncPost(String url, Map<String, String> body, File file, Callback callback) { MultipartBuilder multipartBuilder = new MultipartBuilder(); multipartBuilder.type(MultipartBuilder.MIXED); for (String key : body.keySet()) { multipartBuilder.addFormDataPart(key, body.get(key)); } Date date = new Date(); if (file != null && file.exists()) { //// //图片处理 // BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); // bitmapOptions.inSampleSize = 3; // bitmapOptions.inPreferredConfig= Bitmap.Config.RGB_565; // //// //Bitmap cameraBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), // bitmapOptions); // Bitmap cameraBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), // bitmapOptions); // // /** // * 把图片旋转为正的方向 // */ // Bitmap bitmap = // ImageTools.rotaingImageView(ImageTools.readPictureDegree(file.getAbsolutePath()), // cameraBitmap); // date1 = new Date(); //// byte[] bytes = Bitmap2Bytes(bitmap); // // FileTools.createDirs("萌宝派"); // File sendFile = new // File(Environment.getExternalStorageDirectory().getAbsoluteFile(), File.separator + "萌宝派" + // File.separator + file.getName()); // boolean b = ImageTools.saveBitmap(bitmap, sendFile.getAbsolutePath()); // //图片处理 // if (b) { // multipartBuilder.addFormDataPart("image", "image", // RequestBody.create(MEDIA_TYPE_PNG, sendFile)); // Log.i(TAG, "add picture addres = " + sendFile.getAbsolutePath()); // } multipartBuilder.addFormDataPart( "image", "image", RequestBody.create(MEDIA_TYPE_PNG, getSmallBitmap(file.getPath()))); } RequestBody formBody = multipartBuilder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); enqueue(request, callback); }
private Response multipartPost( String url, StringMap fields, String name, String fileName, RequestBody file, StringMap headers) throws QiniuException { final MultipartBuilder mb = new MultipartBuilder(); mb.addFormDataPart(name, fileName, file); fields.forEach( new StringMap.Consumer() { @Override public void accept(String key, Object value) { mb.addFormDataPart(key, value.toString()); } }); mb.type(MediaType.parse("multipart/form-data")); RequestBody body = mb.build(); Request.Builder requestBuilder = new Request.Builder().url(url).post(body); return send(requestBuilder, headers); }
// post with files public static void asyncPost( String url, Map<String, String> body, ArrayList<String> files, Callback callback) { MultipartBuilder multipartBuilder = new MultipartBuilder(); multipartBuilder.type(MultipartBuilder.MIXED); for (String key : body.keySet()) { multipartBuilder.addFormDataPart(key, body.get(key)); } if (files != null && files.size() > 0) { // if (files.size() > 10) { // for (int i = 0; i < 10; i++) { // //图片处理 // BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); // bitmapOptions.inSampleSize = 8; // Bitmap cameraBitmap = BitmapFactory.decodeFile(files.get(i), // bitmapOptions); // /** // * 把图片旋转为正的方向 // */ // Bitmap bitmap = // ImageTools.rotaingImageView(ImageTools.readPictureDegree(files.get(i)), cameraBitmap); // // // FileTools.createDirs("萌宝派"); // File sendFile = new // File(Environment.getExternalStorageDirectory().getAbsoluteFile(), File.separator + "萌宝派" + // File.separator + new File(files.get(i)).getName()); // boolean b = ImageTools.saveBitmap(bitmap, sendFile.getAbsolutePath()); // //图片处理 // if (b) { // multipartBuilder.addFormDataPart("image" + i, "image" + i, // RequestBody.create(MEDIA_TYPE_PNG, sendFile)); // Log.i(TAG, "add picture addres = " + files.get(i) + "image" + (i + // 1)); // } // } // } else { for (int i = 0; i < files.size(); i++) { // //图片处理 // BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); // bitmapOptions.inSampleSize = 8; // Bitmap cameraBitmap = BitmapFactory.decodeFile(files.get(i), // bitmapOptions); // /** // * 把图片旋转为正的方向 // */ // Bitmap bitmap = // ImageTools.rotaingImageView(ImageTools.readPictureDegree(files.get(i)), cameraBitmap); // FileTools.createDirs("萌宝派"); // File sendFile = new // File(Environment.getExternalStorageDirectory().getAbsoluteFile(), File.separator + "萌宝派" // + File.separator + new File(files.get(i)).getName()); // // boolean b = ImageTools.saveBitmap(bitmap, sendFile.getAbsolutePath()); // 图片处理 // if (b) { multipartBuilder.addFormDataPart( "image" + i, "image" + i, RequestBody.create(MEDIA_TYPE_PNG, getSmallBitmap(files.get(i)))); Log.i(TAG, "add picture addres = " + files.get(i) + "image" + (i + 1)); // } // } } } RequestBody formBody = multipartBuilder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); enqueue(request, callback); }