[英]How do I display my API JSON data into a recycler view
I've just completed putting my API data into a JSONObject, now im trying to visualise the data in a recycler view.我刚刚完成将我的 API 数据放入 JSONObject,现在我试图在回收器视图中可视化数据。 How would I go about by doing this, here is my code:
我将如何去做,这是我的代码:
Where the comment saids pass to adapter, I presume that is where the recycler view would go.评论说传递给适配器的地方,我认为这就是回收者视图的去向。 Sorry for the spaghetti code, I am very new to JAVA and object oriented.
对不起,意大利面代码,我对 JAVA 和面向对象很陌生。 Any help would be greatly appreciated.
任何帮助将不胜感激。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCompanyInput = findViewById(R.id.companyInput);
log = findViewById(R.id.log);
mDescriptionText = findViewById(R.id.descriptionText);
mTitleText = findViewById(R.id.titleText);
save = findViewById(R.id.searchButton);
realm = Realm.getDefaultInstance();
save.setOnClickListener(this);
}
public void onClick(View view) {
//pull string from search query
String companyInput = mCompanyInput.getText().toString();
okHttpClient = new OkHttpClient();
request = new Request.Builder().url(url + companyInput).header("Authorization", "k6DNRbTp-AnQWn51JBz5VuPiTl8jv4_etdzoMyhf").method("GET", null).build();
Log.d(TAG, "onClick:" + url);
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject object = new JSONObject (response.body().string());
JSONArray array = object.getJSONArray("items");
// object.get("start_index");
Log.i(TAG, "onResponse:"+ array.length());
for (int i = 0; i < array.length(); i++){
JSONObject company = (JSONObject) array.get(i);
Log.i(TAG, "onResponse: " + company);
}
//pass to adapter here than feed to recycler view
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// writeToDB(mCompanyInput.getText().toString().trim(), (mDescriptionText.getText().toString().trim()));
showData();
}
public void showData() {
RealmResults<Company> guests = realm.where(Company.class).findAll();
// Use an iterator to invite all guests
String op = "";
for (Company guest : guests) {
op += guest.getName();
op += guest.getAppointments();
}
log.setText(op);
}
public void writeToDB(final String mTitleText, final String mDescriptionText) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
Company user = new Company(mTitleText, mDescriptionText);
bgRealm.insert(user);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
// Transaction was a success.
Log.v("Database", "Data Inserted");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
// Transaction failed and was automatically canceled.
Log.e("Database", error.getMessage());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
Here is what my API is spitting out in the log cat.这是我的 API 在日志猫中吐出的内容。 D/MainActivity: onClick: https://api.companieshouse.gov.uk/search/officers?q= I/MainActivity: onResponse:1 I/MainActivity: onResponse: {"links":{"self":"/officers/gPAqCDv52XAYJON4Z9SHCQOG-hU/appointments"},"description":"Total number of appointments 1","address_snippet":"27 Old Gloucester Street, London, United Kingdom, WC1N 3AX","description_identifiers":["appointment-count"],"appointment_count":1,"snippet":"","kind":"searchresults#officer","address":{"country":"WC1N 3AX","address_line_1":"Old Gloucester Street","premises":"27","locality":"London"},"matches":{"snippet":[],"title":[1,8]},"title":"FACEBOOK ADS SRL"}
D/MainActivity: onClick: https://api.companieshouse.gov.uk/search/officers?q= I/MainActivity: onResponse:1 I/MainActivity: onResponse: {"links":{"self":"/officers /gPAqCDv52XAYJON4Z9SHCQOG-hU/appointments"},"description":"约会总数 1","address_snippet":"27 Old Gloucester Street, London, United Kingdom, WC1N 3AX","description_identifiers":["appointment-count" ],"appointment_count":1,"snippet":"","kind":"searchresults#officer","address":{"country":"WC1N 3AX","address_line_1":"老格洛斯特街","前提":"27","locality":"伦敦"},"matches":{"snippet":[],"title":[1,8]},"title":"FACEBOOK ADS SRL"}
You can find many examples demonstrating what you are looking for.您可以找到许多示例来展示您正在寻找的内容。
https://medium.com/@sontbv/retrofit-2-for-beginners-creating-an-android-api-client-3c4370e1118 https://medium.com/@sontbv/retrofit-2-for-beginners-creating-an-android-api-client-3c4370e1118
And many many others还有很多很多其他的
You may need, first of all, to create a POJO class to parse the response from the server.首先,您可能需要创建一个 POJO 类来解析来自服务器的响应。 You have to implement a custom Adapter for your Recycler View.
你必须为你的 Recycler View 实现一个自定义的 Adapter。
Generally, you will initialize your adapter and recycler view in onCreate and when the data arrives from the server you will just update the content of the adapter.通常,您将在 onCreate 中初始化适配器和回收器视图,当数据从服务器到达时,您只需更新适配器的内容。
There are plenty of tutorials on how to get started with Recycler View, please check out this , this and this .有很多关于如何开始使用 Recycler View 的教程,请查看这个、 这个和这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.