使用JSON解析用户数据

时间:2018-11-29 15:42:30

标签: android

我正在尝试使用JSON解析用户数据。但我的应用程序不断崩溃。

这是我显示的数据 [标题,名字,姓氏,位置{街道,城市,州和邮政编码}和电子邮件]

这是网址 “ http://randomuser.me/api/?results=5&format=json

这是我的代码

用户类别

public class User {
    private String pic;
    private String gender;
    private String title;
    private String first;
    private String last ;
    private String city;
    private String street;
    private String postcode;
    private String email;}

MainActivity

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private OkHttpClient httpClient;
private Request request;
 TextView tv;


private String[] urls = {
        "http://randomuser.me/api/?results=5&format=json"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = findViewById(R.id.data_l);

    httpClient = new OkHttpClient();

    request = new Request.Builder()
            .url(urls[0])
            .build();

    httpClient.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();


        }


        @Override
        public void onResponse(Call call, Response response) throws IOException {

            if(response.isSuccessful())
            {
                final String dataa  = response.body().string();

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        ArrayList<User> users = parseDataJSON(dataa) ;
                        tv.setText(dataa);
                    }
                });
            }

        }
    });


}


public ArrayList<User> parseDataJSON(String data) {

    ArrayList<User> users = new ArrayList<>();

    try {
        JSONObject jsonObject = new JSONObject(data);
        JSONArray usersArray = jsonObject.getJSONArray("results");

        for (int i = 0; i < usersArray.length(); i++) {


            User user = new User();
            JSONObject userObject = usersArray.getJSONObject(i);


            String gender = userObject.getString("gender");
            user.setGender(gender);

            JSONObject nameObject = userObject.getJSONObject("name");

            user.setTitle(nameObject.getString("title"));
            user.setFirst(nameObject.getString("first"));
            user.setLast(nameObject.getString("last"));



            JSONObject locationObject = userObject.getJSONObject("location");


            user.setCity(locationObject.getString("city"));
            user.setStreet(locationObject.getString("street"));

            user.setStreet(locationObject.getString("state"));



            String email = userObject.getString("email");
            user.setEmail(email);




            JSONObject pictureObject  = userObject.getJSONObject("picture");

            user.setPic(pictureObject.getString("large"));



            users.add(user);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return users;

}

我已经执行了。

1 个答案:

答案 0 :(得分:0)

由于您的Pojo具有私有属性(图片,性别,标题等),那么您需要包括(在POJO中)公共设置器来设置数据吗? (您也需要使用吸气剂-才能访问数据)

例如

public void setPic(String pic) {
    this.pic = pic;
}