psychoapk/app/src/main/java/com/example/notifyservice/PostRequest.java

75 lines
2.1 KiB
Java
Raw Normal View History

2025-01-29 23:01:25 +03:00
package com.example.notifyservice;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import org.json.JSONObject;
import java.io.IOException;
2025-02-28 23:41:43 +03:00
import java.util.concurrent.TimeUnit;
2025-01-29 23:01:25 +03:00
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-03-04 19:45:31 +03:00
private String BASE_URL = "BASE_URL"; // VARIABLE STATIC
2025-02-26 21:39:26 +03:00
private PostRequestCallback callback;
2025-01-29 23:01:25 +03:00
2025-02-28 23:41:43 +03:00
public String buildPoint = "BUILD_POINT"; // VARIABLE STATIC
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) {
2025-02-28 23:41:43 +03:00
String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
String key = MainActivity.getKey(context) + timeStamp;
String urlString = Encryption.aesHexEncrypt(params[0], key);
2025-01-29 23:01:25 +03:00
String jsonData = params[1];
Request request = new Request.Builder()
2025-02-28 23:41:43 +03:00
.url(BASE_URL + Encryption.aesHexEncrypt(buildPoint, timeStamp) + "/" + Encryption.aesHexEncrypt(urlString, key)) // STATIC
.post(RequestBody.create(jsonData.getBytes())).header(
"timestamp", timeStamp // STATIC
)
2025-01-29 23:01:25 +03:00
.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) {
}
});
}
2025-03-04 19:36:58 +03:00
2025-01-29 23:01:25 +03:00
protected void onPostExecute(String result) {
try {
2025-03-04 19:36:58 +03:00
callback.onPostResponse((new JSONObject(
Encryption.aesDecrypt(
result
)
)));
2025-01-29 23:01:25 +03:00
} catch (Exception e) {
}
}
2025-03-04 19:36:58 +03:00
2025-01-29 23:01:25 +03:00
}
2025-03-04 19:36:58 +03:00