Я только что закончил помещать свои данные API в JSONObject, теперь я пытаюсь визуализировать данные в представлении переработчика. Как бы я поступил так, вот мой код:
В тех случаях, когда в комментарии говорится о переходнике, я полагаю, что именно там будет отображаться представление переработчика. Извините за код спагетти, я очень новичок в JAVA и ориентирован на объект. Любая помощь будет принята с благодарностью.
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();
}
Вот что мой API выкладывает в журнал cat.
D / MainActivity: onClick: https://api.companieshouse.gov.uk/search/officers?q=
I / MainActivity: onResponse: 1
I / MainActivity: onResponse: {"links": {"self": "/ members / gPAqCDv52XAYJON4Z9SHCQOG-hU / назначений"}, "description": "Общее количество назначений 1", "address_snippet": "27 Old Gloucester Street, Лондон, Великобритания, WC1N 3AX "," description_identifiers ": [" count-count "]," assign_count ": 1," snippet ":" "," kind ":" searchresults # employee "," address ": {" страна ":" WC1N 3AX "," address_line_1 ":" Старая Глостер-стрит "," помещения ":" 27 "," населенный пункт ":" Лондон "}," совпадения ": {" фрагмент ": []," заголовок " : [1,8]}, «title»: «FACEBOOK ADS SRL»}