import okhttp3.OkHttpClient; import okhttp3.FormBody; import okhttp3.Request; import okhttp3.RequestBody; // HashMap with Params HashMap<String, String> params = new HashMap<>(); params.put( "Param1", "A" ); params.put( "Param2", "B" ); // Initialize Builder (not RequestBody) FormBody.Builder builder = new FormBody.Builder(); // Add Params to Builder for ( Map.Entry<String, String> entry : params.entrySet() ) { builder.add( entry.getKey(), entry.getValue() ); } // Create RequestBody RequestBody formBody = builder.build(); // Create Request (same) Request request = new Request.Builder() .url( "url" ) .post( formBody ) .build();
时间: 2024-10-05 05:58:46