60 lines
1.4 KiB
Java
60 lines
1.4 KiB
Java
![]() |
package com.example.notifyservice;
|
||
|
|
||
|
import android.content.Context;
|
||
|
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;
|
||
|
|
||
|
public PostRequest(Context context) {
|
||
|
this.context = context;
|
||
|
}
|
||
|
|
||
|
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(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 {
|
||
|
Log.i("Result", result);
|
||
|
|
||
|
} catch (Exception e) {
|
||
|
}
|
||
|
// Handle the JSON response here
|
||
|
}
|
||
|
}
|