I followed this to Read JSON data in android
over there I am passing result data to another activity
public class MainAct1 extends Activity {
private static String urlString;
private static final String My_TAG= "Log Status";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.data);
if(Dcon.isInternetAvailable(this))
{
try {
urlString = "https://example.net/api_json";
new ProcessJSON(this).execute();
}
catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainAct1.this);
builder.setMessage("Note: Your Server ID is Invalid \n Please check the Server Status");
builder.setTitle("Please Check Server Details");
builder.setPositiveButton("OK",null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
else
{
AlertDialog alertDialog = new AlertDialog.Builder(MainAct1.this).create();
alertDialog.setTitle("Connection Error !");
alertDialog.setMessage("Internet not available, Check your internet connectivity and try again");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
finish();
}
});
alertDialog.show();
}
}
private class ProcessJSON extends AsyncTask<Void, Void, Void> {
public Context context;
String FinalJSonResult;
public ProcessJSON(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler(urlString);
try {
sh.ExecutePostRequest();
if (sh.getResponseCode() == 200) {
FinalJSonResult = sh.getResponse();
if (FinalJSonResult != null) {
try {
JSONObject JObject = new JSONObject(FinalJSonResult);
JSONObject response = JObject.getJSONObject("response");
if(response.has("status")) {
String status = response.getString("status");
MainAct1.this.finish();
Intent op = new Intent(MainAct1.this, MainRes1.class);
op.putExtra("mydata", status);
op.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
op.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
op.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
op.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(op);
}
else {
Toast.makeText(context, "No JSON data", Toast.LENGTH_SHORT).show();
AlertDialog alertDialog = new AlertDialog.Builder(NSdata.this).create();
alertDialog.setTitle("Server Error !");
alertDialog.setMessage("No Data Received");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
finish();
}
});
alertDialog.show();
}
}
catch (JSONException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainAct1.this);
builder.setMessage("Note: Your Server ID is Invalid \n Please check the Server Status");
builder.setTitle("Please Check Server Details");
builder.setPositiveButton("OK",null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
catch (JSONException e) {
}
}
else{
AlertDialog alertDialog = new AlertDialog.Builder(MainAct1.this).create();
alertDialog.setTitle("User Error !");
alertDialog.setMessage("No Data Received");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
finish();
}
});
alertDialog.show();
}
}
else {
Toast.makeText(context, sh.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@Override
public void onBackPressed() {
moveTaskToBack(false);
Toast.makeText(this, "Please wait for a While.. Don't Go back .!", Toast.LENGTH_SHORT).show();
}
}
this is my JSON Data
{"response":{"status":"Active"}}
or
{"response":{"status":"Active:4 in stock"}}
So now I am facing issue with
1.NonJSON some times json data is not received due to server error
or it will show Just some HTML headings...
I need to data Handle it Can any one suggest me.. on this kind
and
2.Empty JSON Data Some times Result may be Null like
{"response":{"status":""}}
or
{"response":{}}
Can Any Suggest me How to Handle those Kinds I already Given Alerts But its not working.
Now I am handling these two kinds of data but Some times I am getting Empty
Update
For a valid JSON data Its Showing Result in next Activity/page in a Text View...
Valid JSON data {"response":{"status":"Active"}}
or {"response":{"status":"Please set the data."}}
I am showing Result... in Next Page
But Sometimes I will get Invalid JSON data like {"response":{"status":}}
or Just HTML page with Welcome text... or OOps page not found ...
So I want to Handle them,,, if I get Invalid JSON Data I want to SHOW the Alert to the USER.... so that's what I am trying But its not working Please Help me on this types
JSONObject JObject = new JSONObject(FinalJSonResult);
JSONObject response = JObject.getJSONObject("response");
// Check Key found or Not
if(response.has("status")) {
String status = response.getString("status");
// Check if Status Empty or Not
if(status.isEmpty()){
}
} else {
//Status Key not found
}
DoInBackground()
only gets executed on a different thread other than the main UI thread.onPostExecute
.ProcessJSON
class use FinalJSonResult
string to handle different situation in onPostExecute().ProcessJSON Class
private class ProcessJSON extends AsyncTask<Void, Void, Void> {
public Context context;
String FinalJSonResult;
public ProcessJSON(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
FinalJSonResult = "";
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler(urlString);
try {
sh.ExecutePostRequest();
if (sh.getResponseCode() == 200) {
FinalJSonResult = sh.getResponse();
if (FinalJSonResult != null) {
try {
JSONObject JObject = new JSONObject(FinalJSonResult);
JSONObject response = JObject.getJSONObject("response");
if(response.has("status")) {
String status = response.getString("status");
MainAct1.this.finish();
Intent op = new Intent(MainAct1.this, MainRes1.class);
op.putExtra("mydata", status);
op.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
op.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
op.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
op.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(op);
}
else {
FinalJSonResult = "No JSON data";
}
}
catch (JSONException e) {
FinalJSonResult = "Your Server ID is Invalid";
}
}
else{
FinalJSonResult = "User Error";
}
}
else {
Toast.makeText(context, sh.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(FinalJSonResult.equalsIgnoreCase("No JSON data")){
// Your AlertDialog code....
}else if(FinalJSonResult.equalsIgnoreCase("Your Server ID is Invalid")){
// Your AlertDialog code....
}else if(FinalJSonResult.equalsIgnoreCase("User Error")){
// Your AlertDialog code....
}
}
}
Please try this
if(response.has("status")) {
// Key found in Response JsonObject
String status = response.getString("status");
} else {
//Status Key not found in Response JsonObject
}
I recommend use library like Gson. and through try/catch & JsonSyntaxException will validate data. but original reason of bug is shape of data. rewrite shape of json data in serverside.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With