Skip to content

Instantly share code, notes, and snippets.

@harilee1325
Created August 30, 2019 21:34
Show Gist options
  • Save harilee1325/4a6da56b481c59484dd7e1eb193c2ae8 to your computer and use it in GitHub Desktop.
Save harilee1325/4a6da56b481c59484dd7e1eb193c2ae8 to your computer and use it in GitHub Desktop.
package com.example.retrofitsimple;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.DeniedByServerException;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static androidx.constraintlayout.widget.Constraints.TAG;
public class MainActivity extends AppCompatActivity {
private Button clickMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickMe = findViewById(R.id.call_api);
clickMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
callApi();
}
});
}
private void callApi() {
ApiInterface apiService = APIClient.getClient().create(ApiInterface.class);
Call<com.example.retrofitsimple.Response> callLoginRetro = apiService.getAllData();
callLoginRetro.enqueue(new Callback<com.example.retrofitsimple.Response>() {
@Override
public void onResponse(Call<com.example.retrofitsimple.Response> call, Response<com.example.retrofitsimple.Response> response) {
String title = response.body().getTitle();
Toast.makeText(getApplicationContext(), title, Toast.LENGTH_SHORT).show();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onFailure(Call<com.example.retrofitsimple.Response> call, Throwable t) {
if (t instanceof DeniedByServerException) {
new android.app.AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("Server not found")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
} else if (t instanceof java.net.SocketTimeoutException) {
new android.app.AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("Timed out")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
} else if (t instanceof java.net.ConnectException) {
new android.app.AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("No internet connection")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
} else{
new android.app.AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("Some error occurred please try after sometime.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
// GrowerUtility.getUtilityInstance().sweetPopup(sweetAlertDialog, mContext, SweetAlertDialog.ERROR_TYPE, error.toString());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment