调用API android后如何返回值(每次调用函数并返回值)?

时间:2019-03-27 06:25:41

标签: java android web-services

下面是我在 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;
}

Activity.java 文件中,当我调用此函数时,它第一次为null;

String token= AppUtill.getToken()   // gives null for first time

2 个答案:

答案 0 :(得分:3)

请尝试界面回调功能

第一步: 创建一个界面

例如。

public interface Result {

    public void success(String result);
}

第二步: 创建一个接口对象

    Result result=new Result() {

                @Override
                public void success(String result) {
                   String token=result;
                    // it will return result here....

                }
            };

将接口对象传递给getToken() 例如。

AppUtill.getToken(result);

您的方法

  public static String getToken(Result result) {

// here your logic to call API

 //after result call
    result.success(result)//pass your result here
    }

答案 1 :(得分:0)

我通过以下解决方案得到了答案:

 public static String getTokenn()  {
    try {
        return new Example().execute().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}

存储在以下功能中:

String token= AppUtill.getTokenn();

并通过以下代码获取价值:

function goBottomRight(){
    
  anime({
    targets: '.btn',
    translateX:  240,
    translateY:  320,
    easing: 'easeInOutQuad'
  }).play;
   
   

}