Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - How to send a String array as basic name value pair as HTTPPOST?

I want to send a array as name value pair as httppost.My server accepts only array values.The following is my code snippet..

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;
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the issue. Here's how:

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

All I had to do was initialize a loop:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...