Java 如何在android中使用截击发送参数数组
我正在开发一个向服务器发送大量数据的应用程序。现在我想使用截取功能向php页面发送一个参数数组。但我无法发送它 用于将参数添加为数组的代码。Java 如何在android中使用截击发送参数数组,java,android,json,android-volley,Java,Android,Json,Android Volley,我正在开发一个向服务器发送大量数据的应用程序。现在我想使用截取功能向php页面发送一个参数数组。但我无法发送它 用于将参数添加为数组的代码。 String[] arr =new String[7]; for(int i=1;i<=7;i++) { arr[i]="questionId_"+i+"_"+"ans_"+i; } HashMap<String ,String[]> params=new HashMap<Strin
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);
String[]arr=新字符串[7];
对于(inti=1;i使用
要在服务器端发送所有值,请获取参数
,然后转换为JSON对象并迭代以获取所有值使用
Map<String, String> postParam = new HashMap<>();
int i=0;
for(String object: friendIds){
postParam.put("friendIds["+(i++)+"]", object);
// you first send both data with same param name as friendnr[] .... now send with params friendnr[0],friendnr[1] ..and so on
}
Map postParam=newhashmap();
int i=0;
for(字符串对象:FriendID){
postParam.put(“friendIds[”+(i++)+“]”,object);
//您首先使用与friendnr[]相同的参数名发送这两个数据……现在使用参数friendnr[0]、friendnr[1]发送,依此类推
}
这是我的作品,希望对你有用。使用谷歌json库制作json数组
compile 'com.google.code.gson:gson:2.6.2'
这段代码将json数组放在请求体中
private void sendTokenToServer(final String jsonArrayString) {
String tag_string_req = "string_req";
String url = Const.SEND_TOKEN_TO_SERVER;
final StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json; charset=utf-8");
return header;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return jsonArrayString.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void sendtoketoserver(最终字符串jsonArrayString){
String tag_String_req=“String_req”;
字符串url=Const.SEND_TOKEN_到_服务器;
final StringRequest strReq=新StringRequest(Request.Method.POST,
url,新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(TAG,response.toString());
hideProgressDialog();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
hideProgressDialog();
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map header=newhashmap();
header.put(“内容类型”,“应用程序/json;字符集=utf-8”);
返回头;
}
@凌驾
公共字符串getBodyContentType(){
返回“application/json;charset=utf-8”;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
返回jsonArrayString.getBytes(“utf-8”);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
返回null;
}
};
AppController.getInstance().addToRequestQueue(streq,标记字符串请求);
}
步骤1
生成json参数
CustomJobjectRequest
class您自己的类?请显示code@prosperK检查我的代码请查看我的编辑答案,这可能是您想要的为什么我不能创建一个在CustomJsonObjectRequest类中接受HashMap的构造函数?@TechGuy:getParams()
或您从Request
类中查看的其他方法是使用Map
而不是HashMap
,因此您应该使用Hash
确定这是发送参数数组的唯一方法吗?@prosper K sir我想在服务器端获得一个参数数组,如中所示php@TechGuy:无法将数组作为参数传递,但可以使用JSONObject结束单个参数中的所有值。使用所有值创建一个JSONObject,然后将其作为HashMap params=new HashMap();params.add(“params”,JSONObject.toString());
现在在php端将收到的字符串从params
转换为JSONObject以获取所有值
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());
Map<String, String> postParam = new HashMap<>();
int i=0;
for(String object: friendIds){
postParam.put("friendIds["+(i++)+"]", object);
// you first send both data with same param name as friendnr[] .... now send with params friendnr[0],friendnr[1] ..and so on
}
compile 'com.google.code.gson:gson:2.6.2'
private void sendTokenToServer(final String jsonArrayString) {
String tag_string_req = "string_req";
String url = Const.SEND_TOKEN_TO_SERVER;
final StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json; charset=utf-8");
return header;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return jsonArrayString.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
Map<String, String> jsonParams = new HashMap<>();
private Set<String> arr;
for(int i=1;i<=7;i++)
{
arr[i]="questionId_"+i+"_"+"ans_"+i;
arr.add("params_"+i,arr[i]);
}
if (arr!= null) {
jsonParams.put("param", arr.toString());
}