2025-01-29 23:01:25 +03:00
|
|
|
package com.example.notifyservice;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2025-02-26 21:39:26 +03:00
|
|
|
import android.util.JsonReader;
|
2025-01-29 23:01:25 +03:00
|
|
|
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;
|
2025-02-26 21:39:26 +03:00
|
|
|
private String BASE_URL = "https://e2df-85-203-39-142.ngrok-free.app/";
|
|
|
|
private PostRequestCallback callback;
|
2025-01-29 23:01:25 +03:00
|
|
|
|
2025-02-26 21:39:26 +03:00
|
|
|
public PostRequest(Context context, PostRequestCallback callback) {
|
2025-01-29 23:01:25 +03:00
|
|
|
this.context = context;
|
2025-02-26 21:39:26 +03:00
|
|
|
this.callback = callback;
|
2025-01-29 23:01:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public final OkHttpClient client = new OkHttpClient();
|
|
|
|
|
|
|
|
public void execute(String... params) {
|
|
|
|
String urlString = params[0];
|
|
|
|
String jsonData = params[1];
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
2025-02-26 21:39:26 +03:00
|
|
|
.url(BASE_URL + urlString)
|
2025-01-29 23:01:25 +03:00
|
|
|
.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 {
|
2025-02-26 21:39:26 +03:00
|
|
|
String hash = (new JSONObject(result)).getString("hash");
|
|
|
|
callback.onPostResponse(hash);
|
2025-01-29 23:01:25 +03:00
|
|
|
} catch (Exception e) {
|
|
|
|
}
|
|
|
|
// Handle the JSON response here
|
|
|
|
}
|
|
|
|
}
|