-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGeminiClient.java
More file actions
81 lines (67 loc) · 3.46 KB
/
GeminiClient.java
File metadata and controls
81 lines (67 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.example.scolarai;
import android.os.Handler;
import android.os.Looper;
import org.json.JSONArray;
import org.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class GeminiClient {
private static final String ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent";
private static final OkHttpClient client = new OkHttpClient();
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
public interface Callback {
void onResponse(String response);
void onError(String error);
}
public static void sendPrompt(String prompt, Callback callback) {
new Thread(() -> {
try {
// Build the JSON structure matching Gemini API format
JSONObject json = new JSONObject();
JSONArray contents = new JSONArray();
JSONObject content = new JSONObject();
JSONArray parts = new JSONArray();
JSONObject part = new JSONObject();
part.put("text", prompt);
parts.put(part);
content.put("parts", parts);
contents.put(content);
json.put("contents", contents);
RequestBody body = RequestBody.create(json.toString(), JSON);
Request request = new Request.Builder()
.url(ENDPOINT)
.addHeader("Content-Type", "application/json")
.addHeader("X-goog-api-key", BuildConfig.GEMINI_API_KEY)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
String respBody = response.body() != null ? response.body().string() : "";
if (response.isSuccessful()) {
// Parse the Gemini API response
JSONObject responseJson = new JSONObject(respBody);
JSONArray candidates = responseJson.optJSONArray("candidates");
if (candidates != null && candidates.length() > 0) {
JSONObject candidate = candidates.getJSONObject(0);
JSONObject contentObj = candidate.getJSONObject("content");
JSONArray partsArray = contentObj.getJSONArray("parts");
String text = partsArray.getJSONObject(0).getString("text");
// Run callback on main thread
new Handler(Looper.getMainLooper()).post(() -> callback.onResponse(text));
} else {
new Handler(Looper.getMainLooper()).post(() -> callback.onError("No response from Gemini"));
}
} else {
String errorMsg = "Error: " + response.code() + " - " + respBody;
new Handler(Looper.getMainLooper()).post(() -> callback.onError(errorMsg));
}
}
} catch (Exception e) {
String errorMsg = e.getMessage();
new Handler(Looper.getMainLooper()).post(() -> callback.onError(errorMsg));
}
}).start();
}
}