在android中解析Json中的错误数据
我正在解析来自php服务器的json数据,但我在每个标记中得到的值不是该标记的实际值。值在每个标记之间切换。 这是我的json:在android中解析Json中的错误数据,android,json,parsing,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", "r
{"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;
}
很明显,我想在Textview
中显示标记“username”
的值,但我在“created\u date”中得到了“created\u date”和“user\u image”。就像这样,所有的值都在切换标签
为了获得更多帮助,我在这里添加了更多的主类和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);
}
}
请指出问题并帮助我解决此问题。请尝试下面的代码,并让我知道它是否有效
@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;
}
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));
你的问题在这里:
OzoneMessageInbox_bean ozoneInboxbean=newozoneMessageInbox_bean(c.getString(TAG_日期)、c.getString(TAG_消息)、c.getString(TAG_用户名)、c.getString(TAG_用户图像))代码>
这与:publicOzoneMessageInbox\uBeans(字符串用户名、字符串用户、字符串图像、字符串消息、字符串创建日期)
@DhruvVaishnav请查看我的最新问题我认为这很完美。
OzoneMessageInbox_Beans ozoneInboxbean = new OzoneMessageInbox_Beans(c.getString(TAG_USERNAME),c.getString(TAG_USERIMAGE),c.getString(TAG_MESSAGE),c.getString(TAG_DATE));