Abhi Bardolia Abhi Bardolia - 4 months ago 17
JSON Question

Show dialog box when json string is not true

public class Recyclerview extends AppCompatActivity {
private RecyclerView mRecyclerView;
CustomAdapter cu;


ArrayList<Employee> arr, arr1;
Toolbar toolbar;

TextView t1, t2;
long l = 0;
private ProgressDialog progress;
int j;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyclerview);

toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);

final RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setHasFixedSize(true);

arr = new ArrayList<Employee>();
arr = InitializeData();

final LinearLayoutManager llm = new LinearLayoutManager(Recyclerview.this);
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);


cu = new CustomAdapter(Recyclerview.this, arr);

final bank ban = new bank(Recyclerview.this);
rv.setAdapter(cu);

registerForContextMenu(rv);


ImageButton refresh = (ImageButton) findViewById(R.id.refresh);

refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progress = ProgressDialog.show(Recyclerview.this, "dialog title",
"dialog message", true);
Toast.makeText(Recyclerview.this, "ok", Toast.LENGTH_LONG).show();


if (isNetworkAvailable()) {

String url = ConstantValues.BASE_URL;
RequestBody formBody = new FormBody.Builder()
.add("key1", "value1")
.add("key2", "value2")
.add("key3", "value3")
.build();

try {
post(url, formBody, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("JSONDemo", "IOException", e);
}

@Override
public void onResponse(final Call call, final Response response) throws IOException {
String JSON = response.body().string();
Log.e("res", " " + JSON);
try {

JSONObject jsonObj = new JSONObject(JSON);



JSONArray resultarr = jsonObj.getJSONArray("result");
final JSONArray resultarr1 = jsonObj.getJSONArray("result1");


if (resultarr1.length() == 0 ) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(Recyclerview.this);
builder1.setMessage("No data found");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});

} else {

ban.OpenDB();
/*Delete old Data from Table*/
ban.Delete("");
for (int i = 0; i < resultarr1.length(); i++) {


Employee emp = new Employee();
JSONObject result1obj = resultarr1.getJSONObject(i);
String result1Id = result1obj.getString(ConstantValues.Bank_ID);
String result1NAME = result1obj.getString(ConstantValues.Bank_NAME);
Log.e("result", " " + result1Id);
Log.e("result", " " + result1NAME);
emp.setId(result1obj.getString(ConstantValues.Bank_ID));
emp.setName(result1obj.getString(ConstantValues.Bank_NAME));
arr.add(emp);

l = ban.InsertQryForTabEmpData(result1Id, result1NAME);
}
ban.CloseDB();

}



runOnUiThread(new Runnable() {
@Override
public void run() {
// you can access all the UI componenet

if (progress.isShowing())
progress.dismiss();
cu.notifyDataSetChanged();

}
});
} catch (Exception e) {

Log.e("JSONDemo", "onResponse", e);
AlertDialog.Builder builder1 = new AlertDialog.Builder(Recyclerview.this);
builder1.setMessage("No data found");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});


}

}
});

} catch (Exception e) {
Log.e("JSONDemo", "Post Exception", e);
}

} else {
Toast.makeText(Recyclerview.this, "Internet not available", Toast.LENGTH_LONG).show();
}


}
});


}

private ArrayList<Employee> InitializeData() {
ArrayList<Employee> arr_emp = new ArrayList<Employee>();
bank ban = new bank(Recyclerview.this);
long l = 0;
ban.OpenDB();
arr_emp = ban.AllSelectQryForTabEmpData();
ban.CloseDB();
return arr_emp;
}


private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}


private final OkHttpClient client = new OkHttpClient();

Call post(String url, RequestBody formBody, Callback callback) throws IOException {

Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();

Call call = client.newCall(request);
call.enqueue(callback);
return call;
}


}


By calling wrong Api it should give dialog box saying unavailable data and is it possible to write dialog box in catch but its not showing dialog box do help me to get rid out of it I dont know where to use if and else...error is coming on
String JSON = response.body().string();
thank you in advance

Answer

Create a method called like showAlertDialog

public void showAlertDialog(String title, String message) {
    final AlertDialog.Builder builder1 = new AlertDialog.Builder(Recyclerview.this);
    builder1.setTitle(title);
    builder1.setMessage(message);
    builder1.setCancelable(true);
    builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.cancel();
        }
    });

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            builder1.show();     
        }
    });
}

Call this method from catch block.