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
- User fills out form in Android app
- User submits form
- App calls SpamBlock API to check submission
- If spam: submission blocked, user notified
- If allowed: submission sent to your backend
- 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/checkendpoint) - Network permissions: Add internet permission to AndroidManifest.xml
- API key: You may need an API key for production use
Common Mistakes
- Trying to use Pixel - Pixel is JavaScript and won't work in Android
- Missing network permission - Add
<uses-permission android:name="android.permission.INTERNET" />to AndroidManifest.xml - Not checking spam first - Always check with SpamBlock API before submitting
- UI thread operations - Use
runOnUiThread()for UI updates from callbacks - Missing error handling - Always handle both success and failure cases
- Base URL - Ensure base URL ends with
/in Retrofit builder
Learn More
- Pixel Getting Started - Learn about the web Pixel (web-only)
- Forms Getting Started - Explore our hosted form solution