CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Patient> patientItems;
private String url = "http://172.30.16.83/test/apps.php";
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Patient> movieItems) {
this.activity = activity;
this.patientItems = movieItems;
}
@Override
public int getCount() {
return patientItems.size();
}
@Override
public Object getItem(int location) {
return patientItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
CirculaireNetworkImageView thumbNail = (CirculaireNetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
Button viewBtn = (Button)convertView.findViewById(R.id.view_btn);
//TextView rating = (TextView) convertView.findViewById(R.id.rating);
//TextView genre = (TextView) convertView.findViewById(R.id.genre);
//TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
Patient m = patientItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
viewBtn.setTag(position);
viewBtn.setOnClickListener(new OnItemClickListener(position));
/*
// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));
// genre
String genreStr = "";
for (String str : m.getGenre()) {
genreStr += str + ", ";
}
genreStr = genreStr.length() > 0 ? genreStr.substring(0,
genreStr.length() - 2) : genreStr;
genre.setText(genreStr);
// release year
year.setText(String.valueOf(m.getYear()));
*/
return convertView;
}
private class OnItemClickListener implements View.OnClickListener {
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
}
@Override
public void onClick(View arg0) {
Patient p=patientItems.get(mPosition);
final String id=p.getTitle();
final String image=p.getThumbnailUrl();
Log.d("Title at row" + mPosition, id);
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("id", id);
jsonParams.put("image", image);
JsonObjectRequest postRequest = new JsonObjectRequest( Request.Method.POST, url,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
Log.e("LOG", error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", System.getProperty("http.agent"));
return headers;
}
};
AppController.getInstance().addToRequestQueue(postRequest);
}
}
}
我遇到了一个问题,我在logcat中显示(json.JSONArray无法转换为JSONObject)。我知道它得到了正确的参数,但由于这个问题,我无法将它们发送到数据库。请给我一些帮助来解决这个问题。谢谢。
01-13 01:43:06.946 20721-20721/info.androidhive.customlistviewvolley E/LOG: com.android.volley.ParseError: org.json.JSONException: Value [{"id":"g"},{"image":"http:\/\/172.30.16.83\/test\/2.png"},{"id":"xd"},{"image":"http:\/\/172.30.16.83\/test\/2.png"},{"id":"hallo"},{"image":"http:\/\/172.30.16.83\/test\/2.png"}] of type org.json.JSONArray cannot be converted to JSONObject
答案 0 :(得分:0)
将JsonObjectRequest
更改为JsonArrayRequest
。因为你的返回json是一个数组。
Exmp。
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", System.getProperty("http.agent"));
return headers;
}
};