package
gfgtestingbyamit.example.gfgtesting;
import
androidx.appcompat.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
org.jetbrains.annotations.NotNull;
import
org.json.JSONException;
import
org.json.JSONObject;
import
java.io.IOException;
import
okhttp3.Call;
import
okhttp3.Callback;
import
okhttp3.OkHttpClient;
import
okhttp3.Request;
import
okhttp3.Response;
public
class
MainActivity
extends
AppCompatActivity {
TextView textView;
Button button;
EditText editText1,editText2;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.result_show);
button = findViewById(R.id.button);
editText1 = findViewById(R.id.input1);
editText2 = findViewById(R.id.input2);
button.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
String s1 = editText1.getText().toString();
String s2 = editText2.getText().toString();
fetchData(s1,s2);
}
});
}
private
void
fetchData(String string1, String string2) {
OkHttpClient client =
new
OkHttpClient();
Request request =
new
Request.Builder()
.url(url)
.get()
.addHeader(
"X-RapidAPI-Key"
,
"Put Your Key Here......."
)
.addHeader(
"X-RapidAPI-Host"
,
"world-time2.p.rapidapi.com"
)
.build();
client.newCall(request).enqueue(
new
Callback() {
@Override
public
void
onFailure(
@NotNull
Call call,
@NotNull
IOException e) {
final
Toast toast = Toast.makeText(MainActivity.
this
,
"GAME OVER!\nScore: "
, Toast.LENGTH_SHORT);
toast.show();
}
@Override
public
void
onResponse(
@NotNull
Call call,
@NotNull
Response response)
throws
IOException {
if
(response.isSuccessful()){
String resp = response.body().string();
MainActivity.
this
.runOnUiThread(
new
Runnable() {
@Override
public
void
run() {
try
{
Toast.makeText(MainActivity.
this
,
"ddff"
, Toast.LENGTH_SHORT).show();
JSONObject jsonObject =
new
JSONObject(resp);
String val1 = jsonObject.getString(
"datetime"
);
textView.setText(val1);
}
catch
(JSONException e) {
e.printStackTrace();
}
}
});
}
}
});
}
}