我想将一个数组作为名称值对发送为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;
}
答案 0 :(得分:17)
我遇到了这个问题。方法如下:
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]));
}
答案 1 :(得分:0)
nameValuePairs.add(new BasicNameValuePair("type", Arrays.toString(type)));
答案 2 :(得分:0)
从数组转换为字符串,然后使用http post发送,再次将服务器端解析从String转换为数组
答案 3 :(得分:0)
json_array = [{param1:&#34; param1Value&#34;,param2:&#34; param2Value&#34;}] 如果你想发送一个带有nameValuePairs的json数组,你可以像这样发送;
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="NppPlugin.DllExport.MSBuild.DllExportTask"
AssemblyFile="NppPlugin.DllExport.MSBuild.dll"/>
<Target Name="AfterBuild"
DependsOnTargets="GetFrameworkPaths"
>
<DllExportTask Platform="$(Platform)"
PlatformTarget="$(PlatformTarget)"
CpuType="$(CpuType)"
EmitDebugSymbols="$(DebugSymbols)"
DllExportAttributeAssemblyName="$(DllExportAttributeAssemblyName)"
DllExportAttributeFullName="$(DllExportAttributeFullName)"
Timeout="$(DllExportTimeout)"
KeyContainer="$(KeyContainerName)$(AssemblyKeyContainerName)"
KeyFile="$(KeyOriginatorFile)"
ProjectDirectory="$(MSBuildProjectDirectory)"
InputFileName="$(TargetPath)"
FrameworkPath="$(TargetedFrameworkDir);$(TargetFrameworkDirectory)"
LibToolPath="$(DevEnvDir)\..\..\VC\bin"
LibToolDllPath="$(DevEnvDir)"
SdkPath="$(SDK40ToolsPath)"/>
</Target>
</Project>