Below is my code in Util.java;
public static String getToken() {
JSONObject postdata = new JSONObject();
try {
AppUtill.getJsonWithHTTPPost(context, 1, new ServiceCallBack() {
@Override
public void serviceCallback(int id, JSONObject jsonResult) {
try {
if (jsonResult.getString("Status").equalsIgnoreCase("Success")) {
JSONObject jsonObject = jsonResult.getJSONObject("Data");
String apptime = jsonObject.optString("apptime");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateandTime = sdf.format(new Date());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Date date1 = dateFormat.parse(apptime);
Date date2 = dateFormat.parse(currentDateandTime);
calendar1.setTime(date1);
calendar2.setTime(date2);
long diff = date1.getTime() - date2.getTime();
long seconds = diff / 1000;
calendar2.add(Calendar.SECOND, (int) seconds);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
new_date = "FoodHIT" + sdf1.format(calendar2.getTime());
new_date = encryptStringmd5(new_date);
System.out.println("Compare Result : " + seconds);
System.out.println("AppTime: " + jsonObject.optString("apptime"));
System.out.println("token : " + new_date);
Prefs.putString("security_token",new_date);
Log.e("NNNNNNNNNNNNNNNN",""+new_date);
} else {
Toast.makeText(context, jsonResult.getString("Message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}, AppUtill.getapptime, postdata);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("new_date........",""+new_date);
return new_date;
}
In Activity.java file when I call this function it gives first time null;
String token= AppUtill.getToken() // gives null for first time