使用凌空柱阵列 [英] Post Array using volley
本文介绍了使用凌空柱阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用volley将类似[1,2,14,15]的内容发布到我的PHP文件中,那里有一个SQL可以从数据库中检索数据.
I Want to post something like this [1,2,14,15] using volley to my PHP file where there's a SQL to retreive data from database.
这是我的抽射:
Map<String, String> userParams = new HashMap<String, String>();
listParams.put(TAG_IDS, all_ids);
CustomRequest myREquest= new CustomRequest(Request.Method.POST, URL, listParams,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
在我的PHP文件中,我将使用类似这样的SQL:
And in my PHP file i'll use a SQL something like this :
$liste=$_POST['IDS'] ;
$val= array_map('intval', $liste);
$new_liste = implode("','", $val);
$query = "SELECT * table WHERE IDS IN('".$new_liste ."')";
此处是我的CustomRequest
public class CustomRequest extends Request<JSONObject> {
private Response.Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params, Response.Listener<JSONObject> reponseListener,
Response.ErrorListener errorListener){
super(Method.GET, url, errorListener);
this.listener=reponseListener;
this.params= params;
}
public CustomRequest(int methode, String url, Map<String, String> params, Response.Listener<JSONObject> reponseListener,
Response.ErrorListener errorListener){
super(methode,url,errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError{
return params;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
}catch (UnsupportedEncodingException e){
return Response.error(new ParseError(e));
}catch (JSONException je){
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
listener.onResponse(response);
}
}
请帮助.
推荐答案
您应该迭代all_ids
并将它们全部放入Map
中,然后将其发送到服务器.例如:
You should iterate your all_ids
and put all of them in Map
which will be send to the server. For example:
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
for(int i=0; i<all_ids.size(); i++) {
params.put(TAG_IDS + "[" + i + "]", all_ids.get(i).toString());
}
return params;
}
这篇关于使用凌空柱阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文