/** * 同步Get(一般不使用) * * @param url * @return String */ public static String syncGet(String url) throws IOException { CacheControl cacheControl = CacheControl.FORCE_NETWORK; Request request = new Request.Builder().url(url).build(); Response response = execute(request); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); Headers responseHeaders = response.headers(); for (int i = 0; i < responseHeaders.size(); i++) { Log.i(TAG, responseHeaders.name(i) + ": " + responseHeaders.value(i)); } Log.i(TAG, "cache response: " + response.cacheResponse()); Log.i(TAG, "network response: " + response.networkResponse()); return response.body().string(); }
// 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); }