如何添加,这是我的代码,意味着我只需要“application / x-www-form-urlencoded”来自服务的此类数据
String postUrl = url;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(postUrl);
request.setHeader("Content-type", "application/x-www-form-urlencoded");
request.setHeader("Expect", "100-continue");
HttpResponse response = client.execute(request);
System.out.println("ddddddddddddddddddddddd "+response);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String line;
builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Log.d("Response", builder.toString());
resObj = builder.toString();
System.out.println("ddddddddddddddddddddddd "+resObj);
} catch (Exception e) {
// TODO: handle exception
Log.d("ERROR", e.toString());
}
答案 0 :(得分:0)
我尝试使用带有namepair值的httpost,它不起作用,所以使用其他方式得出结论
它的工作正常, 注意:我们需要java 1.7 jre
String param1 = "9880938687";
String param2 = "value2";
// ...
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("mobile", param1));
String query = null;
try {
query = String.format("mobile=%s¶m2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URLConnection connection = new URL(targetURL).openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
//InputStream response = connection.getInputStream();
// ..
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println("***"+response.toString());
return response.toString();