Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从android应用程序的方法或其他活动中获取数据_Java_Android_Android Volley - Fatal编程技术网

Java 无法从android应用程序的方法或其他活动中获取数据

Java 无法从android应用程序的方法或其他活动中获取数据,java,android,android-volley,Java,Android,Android Volley,我正在尝试将登录应用程序的用户的数据存储在一个类中,以便我可以在所有活动中使用它的所有数据 我已经遵循了一些关于序列化的指南,但是我无法让它工作 此函数从my api调用用户对象: public void GetGame(String UID){ String url = "https://worldapi.azurewebsites.net/api/homeracer/user/"+UID; final JsonObjectRequest jsonObjectRequest =

我正在尝试将登录应用程序的用户的数据存储在一个类中,以便我可以在所有活动中使用它的所有数据

我已经遵循了一些关于序列化的指南,但是我无法让它工作

此函数从my api调用用户对象:

public void GetGame(String UID){
    String url = "https://worldapi.azurewebsites.net/api/homeracer/user/"+UID;
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,

            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("tag", "jsonresponse" + response.toString());
                    try {
                        //String name = response.getString("userName");
                        currentUser.setUserId(response.getInt("userId"));
                        currentUser.setStartLat(response.getInt("startLat"));
                        currentUser.setStartLong(response.getDouble("startLong"));
                        currentUser.setEndLat(response.getDouble("endLat"));
                        currentUser.setEndLong(response.getDouble("endLong"));
                        currentUser.setUsername(response.getString("userName"));

                        startLong.setText(String.valueOf(currentUser.getStartLong()));
                        userName.setText(currentUser.getUsername());

                        //Intent sendObj = new Intent(Homescreen.this, Homescreen.class);
                        bundle = new Bundle();
                        bundle.putSerializable("userInfo", currentUser);
                        //sendObj.putExtras(bundle);
                        //startActivity(sendObj);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("error", error.toString());
                }
            }
    );
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
            10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    //mRequestQueue.add(jsonObjectRequest);
    Volley.newRequestQueue(this).add(jsonObjectRequest);
}

在Android中,通过实现Parcelableinterface,您可以轻松地在整个活动中传递对象

这些步骤是:

您的类应该实现Parcelable接口。 使用putExtra方法创建一个意图,该意图以要导航并传递对象的活动为目标。 从新活动中检索对象。 下面是实现Parcelable的UserData类。Android Studio帮助您轻松完成此任务

public class UserData implements Parcelable {
    private int UserId;
    private double EndLat, EndLong, StartLat, StartLong;
    private String Username;

    public void setUserId(int _userId) {
        this.UserId = _userId;
    }

    public int getUserId() {
        return UserId;
    }

    public void setEndLat(double _endLat) {
        this.EndLat = _endLat;
    }

    public double getEndLat() {
        return EndLat;
    }

    public void setStartLat(double _startLat) {
        this.StartLat = _startLat;
    }

    public double getStartLat() {
        return StartLat;
    }

    public void setEndLong(double _endLong) {
        this.EndLong = _endLong;
    }

    public double getEndLong() {
        return EndLong;
    }

    public void setStartLong(double _startLong) {
        this.StartLong = _startLong;
    }

    public double getStartLong() {
        return StartLong;
    }

    public void setUsername(String username) {
        this.Username = username;
    }

    public String getUsername() {
        return Username;
    }

    // Parcelable implementation bellow this line

    protected UserData(Parcel in) {
        UserId = in.readInt();
        EndLat = in.readDouble();
        EndLong = in.readDouble();
        StartLat = in.readDouble();
        StartLong = in.readDouble();
        Username = in.readString();
    }

    public static final Creator<UserData> CREATOR = new Creator<UserData>() {
        @Override
        public UserData createFromParcel(Parcel in) {
            return new UserData(in);
        }

        @Override
        public UserData[] newArray(int size) {
            return new UserData[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(UserId);
        parcel.writeDouble(EndLat);
        parcel.writeDouble(EndLong);
        parcel.writeDouble(StartLat);
        parcel.writeDouble(StartLong);
        parcel.writeString(Username);
    }
}

就这样

非常感谢!我在一个活动中得到了这个结果,现在如果我想把这个数据发送到第三个活动,我该怎么做?在第一个活动中,我是否可以在不实际启动第三个活动的情况下发送意图?或者我必须在第二次活动中以某种方式对其进行重新加固,并将其发送给第三次活动?如果这没有意义,很抱歉!:我会将代码发布到我的意思,如果这没有任何意义,你会做完全相同的事情,但你的意图将针对新的活动。Intent Intent=newintentcontext,NewTargetActivity.class。
public class UserData implements Parcelable {
    private int UserId;
    private double EndLat, EndLong, StartLat, StartLong;
    private String Username;

    public void setUserId(int _userId) {
        this.UserId = _userId;
    }

    public int getUserId() {
        return UserId;
    }

    public void setEndLat(double _endLat) {
        this.EndLat = _endLat;
    }

    public double getEndLat() {
        return EndLat;
    }

    public void setStartLat(double _startLat) {
        this.StartLat = _startLat;
    }

    public double getStartLat() {
        return StartLat;
    }

    public void setEndLong(double _endLong) {
        this.EndLong = _endLong;
    }

    public double getEndLong() {
        return EndLong;
    }

    public void setStartLong(double _startLong) {
        this.StartLong = _startLong;
    }

    public double getStartLong() {
        return StartLong;
    }

    public void setUsername(String username) {
        this.Username = username;
    }

    public String getUsername() {
        return Username;
    }

    // Parcelable implementation bellow this line

    protected UserData(Parcel in) {
        UserId = in.readInt();
        EndLat = in.readDouble();
        EndLong = in.readDouble();
        StartLat = in.readDouble();
        StartLong = in.readDouble();
        Username = in.readString();
    }

    public static final Creator<UserData> CREATOR = new Creator<UserData>() {
        @Override
        public UserData createFromParcel(Parcel in) {
            return new UserData(in);
        }

        @Override
        public UserData[] newArray(int size) {
            return new UserData[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(UserId);
        parcel.writeDouble(EndLat);
        parcel.writeDouble(EndLong);
        parcel.writeDouble(StartLat);
        parcel.writeDouble(StartLong);
        parcel.writeString(Username);
    }
}
UserData yourObject = getIntent().getParcelableExtra("your_key");