0

How to load dynamic json file using Gson .... for ex:

ChatItem item = new Gson().fromJson(Helper.loadJSONFromAsset(getApplicationContext(), "http://sample.com/a/chat_message.json"), ChatItem.class);

protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
name = getIntent().getStringExtra(Constant.NAME);
userID = Preference.getPrefString(getApplicationContext(), Constant.USER_ID);
chatUserID = getIntent().getStringExtra(Constant.CHAT_USER_ID);
loadData();
}

private void loadData() {
ChatItem item = new Gson().fromJson(Helper.loadJSONFromAsset(getApplicationContext(), "chat_message.json"), ChatItem.class);

List<ChatItem.Result> chatList;
    if (item.getStatus().equalsIgnoreCase(Constant.SUCCESS)) {
        chatList = item.getResult();
        list = new ArrayList<>();
        if (chatList.size() > 0) {
            for (ChatItem.Result result : chatList) {
                ChatMessage message = new ChatMessage();
                if (result.getId().equalsIgnoreCase(pingMe_id))
                    message.setLeft(false);
                else
                    message.setLeft(true);
                message.setMessage(result.getChat());
                list.add(message);
            }
            adapter = new ChatMessageAdapter(getApplicationContext(), list);
            listView.setAdapter(adapter);
        }
    }
}
  • Is this running code? Either way, it is difficult to understand the issue. – Bryan Sep 22 '16 at 20:24
  • Yes it is running code.. currently json file am fetching from asset folder.. i dont know how to do same thing from dinamic file mean http://sampl.c/a/abc.json file – Manivel Gopi Sep 22 '16 at 20:42
  • What exactly is dynamic about your JSON file? Maybe you can paste an example of the JSON to show what you mean? An [answer to a similar question](http://stackoverflow.com/a/5809667/5115932) shows that you can deserialize JSON into any `Collection` type, including `Map`. Maybe this is what you are looking for? – Bryan Sep 23 '16 at 12:41

0 Answers0