如何在Android中使用Volley发送参数数组 [英] how to send array of params using volley in android
问题描述
我正在开发一个向服务器发送大量数据的应用程序.现在我想使用volley将一组参数发送到php页面.但是我无法发送它.
I am developing an application which send lot of data to server.Now i want to send an array of params to a php page using volley.But i am not able to send it.
用于将参数添加为Array的代码.
String[] arr =new String[7];
for(int i=1;i<=7;i++)
{
arr[i]="questionId_"+i+"_"+"ans_"+i;
}
HashMap<String ,String[]> params=new HashMap<String, String[]>(7);
params.put("params", arr);
向服务器发出请求的代码
RequestQueue que=Volley.newRequestQueue(this);
final ProgressDialog dialog = new ProgressDialog(HealthMyHistory.this);
dialog.setTitle("Please Wait");
dialog.setMessage("Sending Data");
dialog.setCancelable(false);
dialog.show();
CustomJobjectRequest jsObjRequest = new CustomJobjectRequest(Method.POST, url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError response) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Unable to Send Data!"+" "+response.toString(), Toast.LENGTH_SHORT).show();
}
});
que.add(jsObjRequest);
}
Problem is in CustomJobjectRequest there is no constructor available of type in which Hashmap accepts string & array as argument.How to do it ?
代码或CustomJsonObjectRequest
package com.example.healthcoach.data;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
public class CustomJobjectRequest extends Request<JSONObject>{
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomJobjectRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.POST, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomJobjectRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomJobjectRequest(int post, String url,
HashMap<String, String[]> params2, Listener<JSONObject> listener2,
ErrorListener errorListener) {
// TODO Auto-generated constructor stub
}
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected void deliverResponse(JSONObject response) {
listener.onResponse(response);
}
@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));
}
}
}
推荐答案
使用
HashMap<String ,String> params=new HashMap<String, String>(7);
for(int i=1;i<=7;i++)
{
params.put("params_"+i, arr[i]);
}
,因为当前您正在使用 in 要将单个参数中的所有值发送到服务器,请使用 To send all values in single parameter to server use 要在服务器端发送所有值,请获取 TO send all values on server side get 这篇关于如何在Android中使用Volley发送参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!String
类型作为CustomJobjectRequest
类中Map的值,但是在创建CustomJobjectRequest
类的对象时发送String[]
类型.CustomJobjectRequest
class because currently you are using String
type as value in Map in CustomJobjectRequest
class but sending String[]
type when create object of CustomJobjectRequest
class.JSONObject
.使用所有键值创建一个json对象,如下所示:JSONObject
.Create a json object using all key-value as: JSONObject jsonObject=new JSONObject();
for(int i=1;i<=7;i++)
{
arr[i]="questionId_"+i+"_"+"ans_"+i;
jsonObject.put("params_"+i,arr[i]);
}
HashMap<String ,String> params=new HashMap<String, String>();
params.put("params",jsonObject.toString());
params
并转换为JSON对象并进行迭代以获取所有值params
and convert to JSON object and iterate to get all values