Android 带有按钮json post截击的列表视图
将Android 带有按钮json post截击的列表视图,android,listview,post,android-volley,Android,Listview,Post,Android Volley,将JsonObjectRequest更改为JsonArrayRequest。因为返回的json是一个数组 Exmp JsonArrayRequest-JsonArrayRequest=newjsonarrayrequest(Request.Method.POST,url, 新的Response.Listener(){ @凌驾 公共void onResponse(JSONArray响应){ } }, 新的Response.ErrorListener(){ @凌驾 公共无效onErrorRespon
JsonObjectRequest
更改为JsonArrayRequest
。因为返回的json是一个数组
Exmp
JsonArrayRequest-JsonArrayRequest=newjsonarrayrequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}
){
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
headers.put(“用户代理”,System.getProperty(“http.agent”);
返回标题;
}
};
将JsonObjectRequest
更改为JsonArrayRequest
。因为返回的json是一个数组
Exmp
JsonArrayRequest-JsonArrayRequest=newjsonarrayrequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}
){
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
headers.put(“用户代理”,System.getProperty(“http.agent”);
返回标题;
}
};
共享您的logcat错误消息。很抱歉,我解决了此问题,但我遇到了另一个问题,我可以编辑此问题。Thx共享您的logcat错误消息。抱歉,我解决了此问题,但我遇到了另一个问题,我可以编辑此问题。我还有一个问题。有一个错误01-13 04:15:01.004 25408-25408/info.androidhive.customlistviewvolley E/LOG:com.android.volley.ParseError:org.json.JSONException:字符0处的预期文字值/`。这是因为没有把jsonparams放在哪里呢?我还有一个问题。有一个错误01-13 04:15:01.004 25408-25408/info.androidhive.customlistviewvolley E/LOG:com.android.volley.ParseError:org.json.JSONException:字符0处的预期文字值/`。这是因为没有放置jsonparams,所以我应该将其放置在哪里?
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);
}
}
}
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
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;
}
};