Java 如何将字符串数组作为基本名称-值对作为HTTPPOST发送?

Java 如何将字符串数组作为基本名称-值对作为HTTPPOST发送?,java,android,json,http,Java,Android,Json,Http,我想将数组作为名称-值对作为httppost发送。我的服务器只接受数组值。以下是我的代码片段 public String SearchWithType(String category_name, String[] type,int page_no) { String url = "http://myURL"; StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .p

我想将数组作为名称-值对作为httppost发送。我的服务器只接受数组值。以下是我的代码片段

public String SearchWithType(String category_name, String[] type,int page_no) {

    String url = "http://myURL";
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String auth_token = Login.authentication_token;
    String key = Login.key;

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("authentication_token",
                auth_token));
        nameValuePairs.add(new BasicNameValuePair("key", key));
        nameValuePairs.add(new BasicNameValuePair("category_name",
                category_name));
        int i = 0;
        nameValuePairs.add(new BasicNameValuePair("type", type[i]));
        nameValuePairs.add(new BasicNameValuePair("page", String.valueOf(page_no)));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        eu = EntityUtils.toString(entity).toString();

    } catch (IOException ioe) {
        String ex = ioe.toString();
        return ex;
    }

    return eu;
} 
publicstringsearchwithtype(字符串类别名称、字符串[]类型、整型页码){
字符串url=”http://myURL";
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
字符串auth\u token=Login.authentication\u token;
String key=Login.key;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“身份验证\u令牌”),
认证令牌);
添加(新的BasicNameValuePair(“key”,key));
添加(新的BasicNameValuePair(“类别名称”),
类别(名称);;
int i=0;
添加(新的BasicNameValuePair(“类型”,类型[i]);
添加(新的BasicNameValuePair(“page”,String.valueOf(page_no)));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
eu=EntityUtils.toString(entity.toString();
}捕获(ioe异常ioe){
字符串ex=ioe.toString();
退换货;
}
返回欧盟;
} 

我发现了问题。以下是方法:

try {
    int i = 0;

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("authentication_token", auth_token));
    nameValuePairs.add(new BasicNameValuePair("key", key));
    nameValuePairs.add(new BasicNameValuePair("category_name", category_name));
    nameValuePairs.add(new BasicNameValuePair("type", type[i]));
    nameValuePairs.add(new BasicNameValuePair("page", String.valueOf(page_no)));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();

    eu = EntityUtils.toString(entity).toString();
} catch (Exception e) {
    Log.e(TAG, e.toString());
}
试试看{
int i=0;
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“身份验证令牌”,身份验证令牌));
添加(新的BasicNameValuePair(“key”,key));
添加(新的BasicNameValuePair(“类别名称”,类别名称));
添加(新的BasicNameValuePair(“类型”,类型[i]);
添加(新的BasicNameValuePair(“page”,String.valueOf(page_no)));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
eu=EntityUtils.toString(entity.toString();
}捕获(例外e){
Log.e(标记,e.toString());
}
我所要做的就是初始化一个循环:

for (int i = 0; i < type.length; i++) {
    nameValuePairs.add(new BasicNameValuePair("type[]",type[i]));
}
for(int i=0;i
将数组转换为字符串,然后使用http post发送,再次从字符串到数组进行服务器端解析

new BasicNameValuePairs("param[0][param1]","param1Value")
new BasicNameValuePairs("param[0][param2]","param2Value")

我试过了,但是它给出了数组的字符串解释…但是后端需要数组值,而不是数组的字符串解释。我也有同样的问题。在php端,您是如何检索[i]类型数组的?请给我一个提示。普通
$type=$\u GET['type']不起作用。我也有同样的问题。在php端检索类型[I]数组应该做什么?请给我一个提示。普通
$type=$\u GET['type']不起作用。
new BasicNameValuePairs("param[0][param1]","param1Value")
new BasicNameValuePairs("param[0][param2]","param2Value")