Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1.     static AlertDialog makerNetworkAlertDialogError(final Context context, VolleyError error)
  2.     {
  3.         final AlertDialog.Builder builder = new AlertDialog.Builder(context);
  4.         builder.setTitle(R.string.network_error);
  5.  
  6.         NetworkResponse networkResponse = error.networkResponse;
  7.         if (networkResponse == null || networkResponse.data == null)
  8.         {
  9.             builder.setMessage(R.string.joinServer);
  10.             builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener()
  11.             {
  12.                 @Override
  13.                 public void onClick(DialogInterface dialog, int which)
  14.                 {
  15.                     ApiClient.getInstance().signOut(context);
  16.                     Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.jplsystemes.cariatmatthieu.dictee_vocale");
  17.                     launchIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK);
  18.                     context.startActivity(launchIntent);
  19.                     Toast.makeText(context, "The network is available, please log in.", Toast.LENGTH_LONG).show();
  20.                     ApiClient.getInstance().offLine = false;
  21.                 }
  22.             });
  23.             return builder.create();
  24.         }
  25.  
  26.         if (checkConnection(context) == false)
  27.         {
  28.             builder.setMessage(R.string.no_network);
  29.         } else if (networkResponse != null && networkResponse.data != null)
  30.         {
  31.             try
  32.             {
  33.                 JSONObject jsonObject = new JSONObject(new String(networkResponse.data));
  34.                 builder.setMessage(jsonObject.getString("content"));
  35.             } catch (JSONException e)
  36.             {
  37.                 e.printStackTrace();
  38.                 builder.setMessage(R.string.internal_error);
  39.             }
  40.         } else
  41.         {
  42.             builder.setMessage(R.string.internal_error);
  43.             return builder.create();
  44.         }
  45.         if (!(context instanceof LoginActivity) && networkResponse != null && networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED)
  46.         {
  47.             builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener()
  48.             {
  49.                 @Override
  50.                 public void onClick(DialogInterface dialog, int which)
  51.                 {
  52.                     ApiClient.getInstance().signOut(context);
  53.                     Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.jplsystemes.cariatmatthieu.dictee_vocale");
  54.                     launchIntent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NEW_TASK);
  55.                     context.startActivity(launchIntent);
  56.                     Toast.makeText(context, "The network is available, please log in.", Toast.LENGTH_LONG).show();
  57.                     ApiClient.getInstance().offLine = false;
  58.                 }
  59.             });
  60.  
  61.         } else
  62.         {
  63.             try
  64.             {
  65.                 JSONObject jsonObject = new JSONObject(new String(networkResponse.data));
  66.                 JSONArray jsonErrors = jsonObject.getJSONArray("errors");
  67.  
  68.                 String finalString = "";
  69.                 for (int i = 0; i < jsonErrors.length(); i++)
  70.                 {
  71.                     finalString += jsonErrors.getString(i) + "\n";
  72.                 }
  73.                 builder.setMessage(finalString);
  74.             } catch (JSONException e)
  75.             {
  76.                 e.printStackTrace();
  77.                 builder.setMessage(R.string.internal_error);
  78.             }
  79.             builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener()
  80.             {
  81.                 @Override
  82.                 public void onClick(DialogInterface dialog, int which)
  83.                 {
  84.                     dialog.cancel();
  85.                 }
  86.             });
  87.         }
  88.         return builder.create();
  89.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement