Java - SpamBlock API and Forms Integration

Learn how to integrate SpamBlock API and Forms solution with Java Android apps using Retrofit. Complete example with spam checking before form submission.

Overview

This example shows how to integrate both the SpamBlock API and SpamBlock Forms solution with Java Android applications using Retrofit. Note: The SpamBlock Pixel is web-only and cannot run in Android apps. However, you can use the SpamBlock API directly to check submissions for spam, and submit to SpamBlock Forms endpoints.

Note: While you can use any backend endpoint, this example specifically demonstrates using both the SpamBlock API and SpamBlock Forms together. The SpamBlock Forms endpoint (https://api.spamblock.io/f/{form_id}) provides a complete hosted form solution. Learn more about Forms or use your own backend endpoint.

Step 1: Add Retrofit Dependencies

Add to your build.gradle:

dependencies {
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}

Step 2: Create API Interface

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface SpamBlockApi {
    @POST("v1/check")
    Call<SpamCheckResponse> checkSpam(@Body SpamCheckRequest request);
    
    @POST("forms/{form_id}")
    Call<FormSubmissionResponse> submitForm(@Body FormSubmissionRequest request);
}

Step 3: Create Request/Response Models

public class SpamCheckRequest {
    private String email;
    private String message;
    private String name;
    
    // Getters and setters
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
    
    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

public class SpamCheckResponse {
    private boolean allowed;
    private int score;
    
    public boolean isAllowed() { return allowed; }
    public void setAllowed(boolean allowed) { this.allowed = allowed; }
    
    public int getScore() { return score; }
    public void setScore(int score) { this.score = score; }
}

public class FormSubmissionRequest {
    private String email;
    private String message;
    private String name;
    
    // Getters and setters
}

public class FormSubmissionResponse {
    private boolean success;
    
    public boolean isSuccess() { return success; }
    public void setSuccess(boolean success) { this.success = success; }
}

Step 4: Implement Form Submission

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Callback;
import retrofit2.Response;

public class ContactFormActivity extends AppCompatActivity {
    private SpamBlockApi api;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_form);
        
        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.spamblock.io/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
        
        api = retrofit.create(SpamBlockApi.class);
    }
    
    private void submitForm(String name, String email, String message) {
        // First, check for spam
        SpamCheckRequest spamRequest = new SpamCheckRequest();
        spamRequest.setEmail(email);
        spamRequest.setMessage(message);
        spamRequest.setName(name);
        
        api.checkSpam(spamRequest).enqueue(new Callback<SpamCheckResponse>() {
            @Override
            public void onResponse(Call<SpamCheckResponse> call, 
                                 Response<SpamCheckResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().isAllowed()) {
                        // If allowed, submit the form
                        submitToBackend(name, email, message);
                    } else {
                        // Blocked as spam
                        runOnUiThread(() -> {
                            Toast.makeText(ContactFormActivity.this, 
                                "Submission blocked as spam", 
                                Toast.LENGTH_SHORT).show();
                        });
                    }
                }
            }
            
            @Override
            public void onFailure(Call<SpamCheckResponse> call, Throwable t) {
                runOnUiThread(() -> {
                    Toast.makeText(ContactFormActivity.this, 
                        "Error checking spam", 
                        Toast.LENGTH_SHORT).show();
                });
            }
        });
    }
    
    private void submitToBackend(String name, String email, String message) {
        FormSubmissionRequest request = new FormSubmissionRequest();
        request.setName(name);
        request.setEmail(email);
        request.setMessage(message);
        
        api.submitForm(request).enqueue(new Callback<FormSubmissionResponse>() {
            @Override
            public void onResponse(Call<FormSubmissionResponse> call, 
                                 Response<FormSubmissionResponse> response) {
                if (response.isSuccessful()) {
                    runOnUiThread(() -> {
                        Toast.makeText(ContactFormActivity.this, 
                            "Submission successful!", 
                            Toast.LENGTH_SHORT).show();
                    });
                }
            }
            
            @Override
            public void onFailure(Call<FormSubmissionResponse> call, Throwable t) {
                runOnUiThread(() -> {
                    Toast.makeText(ContactFormActivity.this, 
                        "Error submitting form", 
                        Toast.LENGTH_SHORT).show();
                });
            }
        });
    }
}

What This Example Shows

  • How to integrate SpamBlock API with Java Android apps
  • Using Retrofit for HTTP requests
  • Spam checking before form submission
  • Async request handling with callbacks

When to Use It

Use this approach when:

  • You're building a Java Android application
  • You're using Retrofit for API calls
  • You need spam protection in native apps
  • You want to check submissions before sending to backend

How it Works

  1. User fills out form in Android app
  2. User submits form
  3. App calls SpamBlock API to check submission
  4. If spam: submission blocked, user notified
  5. If allowed: submission sent to your backend
  6. Success/error feedback shown to user

Important Notes

  • Pixel is web-only: The SpamBlock Pixel cannot run in Android apps
  • API integration: Use the SpamBlock API directly (/v1/check endpoint)
  • Network permissions: Add internet permission to AndroidManifest.xml
  • API key: You may need an API key for production use

Common Mistakes

  1. Trying to use Pixel - Pixel is JavaScript and won't work in Android
  2. Missing network permission - Add <uses-permission android:name="android.permission.INTERNET" /> to AndroidManifest.xml
  3. Not checking spam first - Always check with SpamBlock API before submitting
  4. UI thread operations - Use runOnUiThread() for UI updates from callbacks
  5. Missing error handling - Always handle both success and failure cases
  6. Base URL - Ensure base URL ends with / in Retrofit builder

Learn More