【问题标题】:Getting Null value from 1 class to other after storing value object存储值对象后从 1 个类获取 Null 值到其他类
【发布时间】:2016-10-06 14:10:12
【问题描述】:

我有 Authentication 类,它有 Auth 方法和 2 个参数。 在调用该方法后,由于 aSync,生成了一个凌空请求并通过其他函数捕获响应。该对象是一个静态对象,可以在其他类中访问,但在创建对象或在活动的创建上初始化它之后总是显示 null。

让我们检查一下我的 Authenticate 类:

public class Authenticate {
    private static final String URL = "http://allskkc/zaigham/idsrs_authentication.php";
    public static JSONObject finalresult;

    public Authenticate() {
    }

    public static void Auth(String IEMI, String PIN) throws TimeoutException {
        final JSONObject params = new JSONObject();
        finalresult = new JSONObject();
        RequestQueue queue = Volley.newRequestQueue(Splash.context);
        try {
            params.put("iemi", IEMI);
            params.put("pin", PIN);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("params to server", params.toString());
        JsonObjectRequest jsOBJRequest = new JsonObjectRequest
            (Request.Method.POST, URL, params, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.e("response from server", response.toString());
                    ftn(response);
               }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Response error",error.toString());
                }
            });
        queue.add(jsOBJRequest);
    }

    public static void ftn(JSONObject jsonObject) {
        finalresult = jsonObject;
        Log.e("as", "Response in ftn() = " + finalresult.toString());
    }
}

final finalresult 在我调用 Auth() 方法时具有价值,但在我的 Login 类中它显示 {}

让我们检查一下我的方法调用:

try {
    Authenticate.Auth("358607051299527","1122");
} catch (TimeoutException e) {
    e.printStackTrace();
}
Log.e("as","Prefrences Saved");
Log.e("as","My final result = "+Authenticate.finalresult.toString());

我附上了我的 Log.e 图片,这可能有助于更多地理解。

【问题讨论】:

  • 为什么你把错误截图而不是复制粘贴?

标签: android json android-studio android-volley


【解决方案1】:

您可以在日志中看到应用程序在请求完成之前尝试获取请求的结果。网络请求是异步的,它是在后台线程上完成的。

您应该在请求完成后通过某种回调获得结果。

【讨论】:

  • 我如何解决这个问题@sergey
猜你喜欢
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 2015-11-25
  • 2018-09-09
  • 2013-05-10
  • 1970-01-01
  • 2022-01-27
  • 2013-11-02
相关资源
最近更新 更多