Je souhaite envoyer un tableau sous forme de paire nom-valeur en tant que httppost.Mon serveur n'accepte que les valeurs de tableau.
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;
}
J'ai eu le problème. Voici comment:
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());
}
Tout ce que j'avais à faire était d'initialiser une boucle:
for (int i = 0; i < type.length; i++) {
nameValuePairs.add(new BasicNameValuePair("type[]",type[i]));
}
json_array = [{param1: "param1Value", param2: "param2Value"}] si vous voulez envoyer un tableau Json avec nameValuePairs, vous pouvez envoyer comme ceci;
new BasicNameValuePairs("param[0][param1]","param1Value")
new BasicNameValuePairs("param[0][param2]","param2Value")
nameValuePairs.add(new BasicNameValuePair("type", Arrays.toString(type)));
convertir d'un tableau en chaîne puis envoyer à l'aide de la publication http, à nouveau une analyse côté serveur de chaîne en tableau