package com.example.notifyservice; import android.content.Context; import android.util.JsonReader; import android.util.Log; import androidx.annotation.NonNull; import org.json.JSONArray; import org.json.JSONObject; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class PostRequest { private Context context; private String BASE_URL = "https://e2df-85-203-39-142.ngrok-free.app/"; private PostRequestCallback callback; public PostRequest(Context context, PostRequestCallback callback) { this.context = context; this.callback = callback; } public final OkHttpClient client = new OkHttpClient(); public void execute(String... params) { String urlString = params[0]; String jsonData = params[1]; Request request = new Request.Builder() .url(BASE_URL + urlString) .post(RequestBody.create(jsonData.getBytes())) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { onPostExecute(response.body() != null ? response.body().string() : ""); } public void onFailure(@NonNull Call call, @NonNull IOException e) { } }); } protected void onPostExecute(String result) { try { String hash = (new JSONObject(result)).getString("hash"); callback.onPostResponse(hash); } catch (Exception e) { } // Handle the JSON response here } }