问题 如何将String数组作为基本名称值对发送为HTTPPOST?


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

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;
} 

1797
2018-01-15 07:02


起源

我也有同样的问题。你在php端做了什么来检索type [i]数组?请提示一下。正常 $type= $_GET['type']; 不起作用。 stackoverflow.com/questions/22611213/... - Namikaze Minato


答案:


我遇到了这个问题。就是这样:

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());
}

我所要做的就是初始化一个循环:

for (int i = 0; i < type.length; i++) {
    nameValuePairs.add(new BasicNameValuePair("type[]",type[i]));
}

15
2018-01-16 05:58



thnx man .. for循环工作.. - Rana Ranvijay Singh
我也有同样的问题。我应该在php端做什么来检索type [i]数组?请提示一下。正常 $type= $_GET['type']; 不起作用。 stackoverflow.com/questions/22611213/... - Namikaze Minato


nameValuePairs.add(new BasicNameValuePair("type", Arrays.toString(type)));

0
2018-01-15 07:08



我试过,但它给出了数组的字符串解释...但是后端需要数组值而不是字符串对该数组的解释 - goonerDroid


从数组转换为字符串,然后使用http post发送,再次从String到数组的服务器端解析


0
2018-01-16 07:27





json_array = [{param1:“param1Value”,param2:“param2Value”}] 如果你想发送一个带有nameValuePairs的json数组,你可以像这样发送;

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

0
2018-06-12 12:12