When the http server (on AndroidApp) receives a request, I show an alert dialog to the user about this request. After the user responds to this alert dialog, I want it to return to the client (Browser).
I also want to add a 10 second timeout in case the user doesn't press any button.
`
private HttpServerManager() {
try {
InetSocketAddress address = new InetSocketAddress(8080);
httpServer = HttpServer.create(address, 0);
httpServer.createContext("/getDeviceRegister", new EchoGetHandlerForDeviceRegister());
httpServer.setExecutor(null);
httpServer.start();
Log.i(TAG, "HttpServer Start");
} catch (Exception e) {
e.printStackTrace();
}
}
`
`
class EchoGetHandlerForDeviceRegister implements HttpHandler {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void handle(HttpExchange he) throws IOException {
// parse request
Map<String, Object> parameters = new HashMap<String, Object>();
URI requestedUri = he.getRequestURI();
String query = requestedUri.getRawQuery();
HttpServerManager.parseQuery(query, parameters);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
//SHOW DIALOG HERE
TestApplication.instance().showAdminRegisterDialog(he.getRemoteAddress());
}
});
// send response
String response = "<h1>Device Register</h1>";
for (String key : parameters.keySet())
response += key + " = " + parameters.get(key) + "\n";
he.sendResponseHeaders(200, response.length());
OutputStream os = he.getResponseBody();
os.write(response.toString().getBytes());
os.close();
}
}
`
`
public void showAdminRegisterDialog(InetSocketAddress clientAdress){
Log.i(TAG, "showAdminRegisterDialog()");
if (adminRegisterDialog != null)
adminRegisterDialog.cancel();
Context context = MainActivity.instance();
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.NewDialog2);
builder = new AlertDialog.Builder(context);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
adminRegisterView = li.inflate(R.layout.register_dialog, null);
builder.setView(adminRegisterView);
builder.setCancelable(false);
TextView deviceNameText = adminRegisterView.findViewById(R.id.deviceNameText);
TextView infoText = adminRegisterView.findViewById(R.id.infoText);
deviceNameText.setText(clientAdress.toString());
infoText.setText(R.string.register_admin_allow_text);
AppCompatButton allowButton = adminRegisterView.findViewById(R.id.allowButton);
AppCompatButton notAllowButton = adminRegisterView.findViewById(R.id.notAllowButton);
allowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG,"allowButton");
adminRegisterDialog.dismiss();
}
});
notAllowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG,"not allowButton");
adminRegisterDialog.dismiss();
}
});
adminRegisterDialog = builder.create();
adminRegisterDialog.show();
adminRegisterDialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
`
-I tried using AsyncTask but I couldn't because it was too complicated. -Thread.sleep didn't work as it stopped all processes.
I solved the problem as follows: I am using Alertdialog in class where I defined HttpServer. I keep it in a while loop until the user responds when the alertdialog is shown. After the user click alertdialog button, I finish the while loop and send a response to the client.
boolean isClick = false;
//SHOW ALERT DIALOG HERE
//ALERT DILOG CLICK LISTENER
result.getAllowButton().setOnClickListener(new View.OnClickListener() {
isClick = true;
});
while (!isClick) {
Log.i(TAG, "in while loop");
}
Log.i(TAG, "out while loop");
isClick = false;
// send response
String response = "<h1>Alert Dialog Clicked</h1>";
for (String key : parameters.keySet())
response += key + " = " + parameters.get(key) + "\n";
he.sendResponseHeaders(200, response.length());
OutputStream os = he.getResponseBody();
os.write(response.toString().getBytes());
os.close();