Devraj
Devraj

Reputation: 1531

Parsing wrong data from Json in android

I am parsing the json data from php server but what value I am getting in each tag is not the actual value of that tag. Value get switched between each tags. This is my json:

 {"feed":[{"userid":"7",
"username":"anurag",
"user_image":"http:\/\/xxxxxxxxxxxxxxxxx\/avatars\/ozone\/7_30_7_150_WP_20140330_044.jpg",
"gender":"1",
"is_read":"1",
"sender_id":"7",
"receiver_id":"11",
"message_id":"67",
"message":"hi ",
"created_date":"2015-06-26 12:43:20",
"r_id":"67",
"status":"0"}]}

I am trying to get these values in custom Adapter class like this:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        View vi = convertView;
        ViewHolder holder;

        if(convertView==null)

            convertView = inflater.inflate(R.layout.ozone_message_inboxitem, null);

        holder = new ViewHolder();

        holder.ozoneMsgInboxfrom = (TextView)convertView.findViewById(R.id.ozoneMsgInboxfrom);
        holder.ozonemsginboxContent1 = (TextView)convertView.findViewById(R.id.ozonemsginboxContent);
        holder.ozonemsginboxdate1 = (TextView)convertView.findViewById(R.id.ozonemsginboxdate);
        holder.ozonemsginboxPhoto1 = (ImageView)convertView.findViewById(R.id.ozonemsginboxPhoto);

        holder.ozonemsginboxdate1.setText(feedList.get(position).getUsername());
        //holder.ozonemsginboxContent1.setText(feedList.get(position).getMessage());
        holder.ozoneMsgInboxfrom.setText(feedList.get(position).getCreated_date());

         int rounded_value = 120; 
         DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(rounded_value)).build();
         ImageLoader.getInstance().displayImage(feedList.get(position).getCreated_date(), holder.ozonemsginboxPhoto1,options);

        return convertView;

    }
    public class ViewHolder {

        public ImageView ozonemsginboxPhoto1;
        TextView  ozoneMsgInboxfrom, ozonemsginboxContent1,ozonemsginboxdate1;
    }

As it is clear that I want to display the value of tag "username" in Textview but I am getting "created_date" and "user_image" in "created_date". And like this, all the values are switching there tags.

For more help, I am adding more Main class and Bean class here::

public class OzoneMessageInboxFragment extends Fragment{

private ProgressDialog pDialog;

    OzoneMessageInboxAdapter adapter;
    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String, String>> inboxList;
    ArrayList<OzoneMessageInbox_Beans> feedList = new ArrayList<OzoneMessageInbox_Beans>();
    // products JSONArray
    JSONArray inbox;
    JSONArray inboxImage;
    private PullToRefreshListView ozone_msginbox_listView;
    // Inbox JSON url
    private static final String INBOX_URL = "http://example.com/omessageinbox/id/";

    // ALL JSON node names
    private static final String TAG_FEED = "feed";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_USERIMAGE = "user_image";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_DATE = "created_date";

    AppPreference mAppPreference;
    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ozone_message_inbox, container, false);
        initialiseNoramlVariable();
        initialiseView(view);
        return view;

    }
    private void initialiseView(View view) {

        ozone_msginbox_listView = (PullToRefreshListView)view.findViewById(R.id.ozoneMessageInbox);

        // Hashmap for ListView
        inboxList = new ArrayList<HashMap<String, String>>();

        ozone_msginbox_listView.setOnRefreshListener(new OnRefreshListener(){

            @Override
            public void onRefresh() {

                ozone_msginbox_listView.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ozone_msginbox_listView.onRefreshComplete();
                        feedList = new ArrayList<OzoneMessageInbox_Beans>();
                        new LoadinboxmsgOzone().execute();
                        adapter.notifyDataSetChanged();
                    }
                }, 2000);
            }
        });
        new LoadinboxmsgOzone().execute();
    }

    /**
     * Background Async Task to Load all INBOX messages by making HTTP Request
     * */
    class LoadinboxmsgOzone extends AsyncTask<String, String, String> {

        public String finalValue = "";

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(context);
            pDialog.setMessage("Loading Inbox ...");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();


            //http://m.sabakuch.com/api/omessageinbox/id/11/userid/11/key/ee8b3de3507e194e8c43eb9458bf728e
            String url = INBOX_URL+mAppPreference.getUserID()+"/"+"userid"+"/"+mAppPreference.getUserID()+"/"+"key"+"/"+mAppPreference.getServerKey();
            Log.d("urlll", url);
            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(url, "GET",params);
            Log.d("general JSON ", json.toString());
            try {
                inbox = json.getJSONArray(TAG_FEED);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    OzoneMessageInbox_Beans ozoneInboxbean = new OzoneMessageInbox_Beans(c.getString(TAG_DATE),c.getString(TAG_MESSAGE),c.getString(TAG_USERNAME),c.getString(TAG_USERIMAGE));

                    feedList.add(ozoneInboxbean);
                    }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        @SuppressWarnings("unchecked")
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            adapter = new OzoneMessageInboxAdapter(feedList,context);
            ozone_msginbox_listView.setAdapter(adapter);
        }
    }
    private void initialiseNoramlVariable() {
        context  = getActivity();
        mAppPreference = AppPreference.getInstance(context);
    }

}

and my bean class is::

public class OzoneMessageInbox_Beans implements Serializable{

    String userid;
    String username;
    String user_image;
    String gender;
    String is_read;
    String sender_id;
    String receiver_id;
    String message_id;
    String message;
    String created_date;
    String r_id;
    String status;
    public OzoneMessageInbox_Beans(String username, String user_image, String message, String created_date) {

        super();
        this.username = username;
        this.user_image = user_image;
        this.message = message;
        this.created_date = created_date;
    }
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUser_image() {
        return user_image;
    }
    public void setUser_image(String user_image) {
        this.user_image = user_image;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getIs_read() {
        return is_read;
    }
    public void setIs_read(String is_read) {
        this.is_read = is_read;
    }
    public String getSender_id() {
        return sender_id;
    }
    public void setSender_id(String sender_id) {
        this.sender_id = sender_id;
    }
    public String getReceiver_id() {
        return receiver_id;
    }
    public void setReceiver_id(String receiver_id) {
        this.receiver_id = receiver_id;
    }
    public String getMessage_id() {
        return message_id;
    }
    public void setMessage_id(String message_id) {
        this.message_id = message_id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getCreated_date() {
        return created_date;
    }
    public void setCreated_date(String created_date) {
        this.created_date = created_date;
    }
    public String getR_id() {
        return r_id;
    }
    public void setR_id(String r_id) {
        this.r_id = r_id;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }


}

Please point out the issue and help me to solve this.

Upvotes: 0

Views: 108

Answers (3)

vontell
vontell

Reputation: 342

Your problem is here: OzoneMessageInbox_Beans ozoneInboxbean = new OzoneMessageInbox_Beans(c.getString(TAG_DATE),c.getString(TAG_MESSAGE),c.getString(TAG_USERNAME),c.getString(TAG_USERIMAGE));

Which correlates to: public OzoneMessageInbox_Beans(String username, String user_image, String message, String created_date)

Upvotes: 1

Dhruv
Dhruv

Reputation: 1882

There is mistake in your constructor in for loop.

You are doing

OzoneMessageInbox_Beans ozoneInboxbean = new OzoneMessageInbox_Beans(c.getString(TAG_DATE),c.getString(TAG_MESSAGE),c.getString(TAG_USERNAME),c.getString(TAG_USERIMAGE));

changed it to following one.

OzoneMessageInbox_Beans ozoneInboxbean = new OzoneMessageInbox_Beans(c.getString(TAG_USERNAME),c.getString(TAG_USERIMAGE),c.getString(TAG_MESSAGE),c.getString(TAG_DATE));

Upvotes: 6

Vaisakh N
Vaisakh N

Reputation: 790

Try with this code below and let me know if it's work

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View vi = convertView;
        ViewHolder holder;

        if(convertView==null)
{
            convertView = inflater.inflate(R.layout.ozone_message_inboxitem, null);

        holder = new ViewHolder();

        holder.ozoneMsgInboxfrom = (TextView)convertView.findViewById(R.id.ozoneMsgInboxfrom);
        holder.ozonemsginboxContent1 = (TextView)convertView.findViewById(R.id.ozonemsginboxContent);
        holder.ozonemsginboxdate1 = (TextView)convertView.findViewById(R.id.ozonemsginboxdate);
        holder.ozonemsginboxPhoto1 = (ImageView)convertView.findViewById(R.id.ozonemsginboxPhoto);
    convertView.setTag(holder );
} else {
    holder = (ViewHolder) convertView.getTag();
}
holder.ozonemsginboxdate1.setText(feedList.get(position).getUsername());
        //holder.ozonemsginboxContent1.setText(feedList.get(position).getMessage());
        holder.ozoneMsgInboxfrom.setText(feedList.get(position).getCreated_date());

         int rounded_value = 120; 
         DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(rounded_value)).build();
         ImageLoader.getInstance().displayImage(feedList.get(position).getCreated_date(), holder.ozonemsginboxPhoto1,options);

        return convertView;

    }
    public class ViewHolder {

        public ImageView ozonemsginboxPhoto1;
        TextView  ozoneMsgInboxfrom, ozonemsginboxContent1,ozonemsginboxdate1;
    }

Upvotes: 0

Related Questions