Добрый день. Я получил ответ сервера передал его во фрагмент
public void Okhhtp(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://link")
.build();
Call call = client.newCall(request);
call.enqueue(new
Callback() {
@Override
public void onFailure (Call call, IOException e){
Toast.makeText(MainActivity.this, "Text loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse (Call call, Response response) throws IOException {
final String result = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
dialogF.setResult(result);
Toast.makeText(MainActivity.this,result, Toast.LENGTH_SHORT).show();
}
});
}
});
}
Во фрагменте я его получил и отправил в адаптер но при запуске лист вью ничего не выдает.
public void setResult(String result) {
//JSONObject jsonObject = null;
if (result != null) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("dialog");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject productObject = jsonArray.getJSONObject(i);
UpList.add(new DialogSaveData(
productObject.getString("avatar"),
productObject.getString("name"),
productObject.getString("text"),
productObject.getString("state")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
CastomDialog adapter = new CastomDialog(getActivity(), R.layout.castom_dialog_list, UpList);
// присваиваем адаптер списку
dialogList.setAdapter(adapter);
}
}
Я попробовал создать строку в которую вписал json
res = "{\"dialog\": [{\"name\": \"2\",\"avatar\": \"2",\"text\": \"2\",\"state\": \"0\"},]}";
и подсунул ее вместо result, все хорошо. Начал сравнивать что приходит с сервера и эту строку, они одинаковы но при этом json из result не парсится. Подскажите с чем это может быть связано?
код адаптера
public class CastomDialog extends ArrayAdapter<DialogSaveData> {
ArrayList<DialogSaveData> products;
Context context;
int resorse;
String decode, a;
public CastomDialog(@NonNull Context context, @LayoutRes int resource, @NonNull List<DialogSaveData> objects) {
super(context, resource, objects);
this.products = products;
this.context = context;
this.resorse = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.castom_dialog_list, null, true);
}
DialogSaveData product = getItem(position);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView2);
Glide.with(context).load(product.getImage()).into(imageView);
TextView textView = (TextView) convertView.findViewById(R.id.name);
textView.setText(product.getName());
TextView mess = (TextView) convertView.findViewById(R.id.mess);
a = product.getMess();
//try {
// decode = URLDecoder.decode(a, "UTF8");
//} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
mess.setText(a);
//String new1 = product.getState().replace("1","New");
//String new0 = new1.replace("0","");
// TextView state = (TextView) convertView.findViewById(R.id.state);
// state.setText(new0);
return convertView;
}
}
notifyDataSetChanged()
у адаптера.