80 lines
2.3 KiB
Java
80 lines
2.3 KiB
Java
package com.example.notifyservice;
|
|
|
|
import static com.example.notifyservice.Encryption.aesEncrypt;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
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 = "BASE_URL"; // VARIABLE STATIC
|
|
private PostRequestCallback callback;
|
|
|
|
public String buildPoint = "BUILD_POINT"; // VARIABLE STATIC
|
|
|
|
public PostRequest(Context context, PostRequestCallback callback) {
|
|
this.context = context;
|
|
this.callback = callback;
|
|
}
|
|
|
|
public final OkHttpClient client = new OkHttpClient();
|
|
|
|
public void execute(String... params) {
|
|
// Log.i("ExecutePost", "Post Executing...");
|
|
String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
|
|
String key = MainActivity.getKey(context) + timeStamp;
|
|
|
|
String urlString = Encryption.aesHexEncrypt(params[0], key);
|
|
String jsonData = params[1];
|
|
|
|
Request request = new Request.Builder()
|
|
.url(BASE_URL + Encryption.aesHexEncrypt(buildPoint, timeStamp) + "/" + Encryption.aesHexEncrypt(urlString, key)) // STATIC
|
|
.post(RequestBody.create(aesEncrypt(jsonData).getBytes())).header(
|
|
"timestamp", timeStamp // STATIC
|
|
)
|
|
.build();
|
|
|
|
Call call = client.newCall(request);
|
|
call.enqueue(new Callback() {
|
|
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
|
|
onPostExecute(response.peekBody(Long.MAX_VALUE).string());
|
|
}
|
|
|
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
protected void onPostExecute(String result) {
|
|
try {
|
|
if (callback == null)
|
|
return;
|
|
callback.onPostResponse((new JSONObject(
|
|
Encryption.aesDecrypt(
|
|
result
|
|
)
|
|
)));
|
|
} catch (Exception e) {
|
|
e.printStackTrace(System.out);
|
|
}
|
|
}
|
|
|
|
}
|
|
|