解析Android中Json的错误数据

时间:2015-07-07 12:47:49

标签: android json parsing

我正在从php服务器解析json数据但是我在每个标记中得到的值不是该标记的实际值。在每个标签之间切换值。 这是我的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"}]}

我试图在自定义适配器类中获取这些值,如下所示:

@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;
    }

很明显,我想在"username"中显示标记Textview的值,但我在“created_date”中显示“created_date”和“user_image”。像这样,所有值都在切换标签。

要获得更多帮助,我在这里添加了更多Main类和Bean类::

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);
    }

}
  

我的bean类是::

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;
    }


}

请指出问题并帮助我解决这个问题。

3 个答案:

答案 0 :(得分:6)

你的构造函数在for循环中有错误。

你在做什么

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

将其更改为以下一个。

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

答案 1 :(得分:1)

您的问题出在此处:OzoneMessageInbox_Beans ozoneInboxbean = new OzoneMessageInbox_Beans(c.getString(TAG_DATE),c.getString(TAG_MESSAGE),c.getString(TAG_USERNAME),c.getString(TAG_USERIMAGE));

public OzoneMessageInbox_Beans(String username, String user_image, String message, String created_date)

相关

答案 2 :(得分:0)

尝试使用以下代码,让我知道它的工作是否正常

 @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;
    }