我试图进行http - post call。
我发给他这个请求的服务器需要有两个参数
1.第一个参数是Int
2.第二个参数是我作为字符串发送的枚举
我尝试以两种方式做到这两种方式都失败了:
第一种方式:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("intVal", "-100"));
nameValuePairs.add(new BasicNameValuePair("enumVal", "enumAsString"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
我得到'酒吧请求'作为回应。
第二种方式:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SERVER_ADDRESS + METHOD_NAME);
try
{
HttpParams postParams = new BasicHttpParams();
postParams.setIntParameter("intVal", -100 );
postParams.setParameter ("enumVal" , "enumAsString" );
httppost.setParams(postParams);
HttpResponse p = httpclient.execute(httppost);
}
catch(Exception e)
{
....
}
通过这种方式,我看到服务器获取参数 - 但第一个参数(需要为int)为0,第二个参数(需要为枚举)也为0。
P.S:在服务器上运行的代码是WCF代码 - 使用REST。
请..我必须解决这个问题...
感谢。
答案 0 :(得分:2)
这适用于我的情况。试试这个,让我知道发生了什么......
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("uname","test"));
nameValuePair.add(new BasicNameValuePair("pws","test"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
答案 1 :(得分:1)
使用Andorid,您可以使用HttpPost
和HttpUrlConnection
两种方式发送参数:
第一种方式:
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> postParameters;
httpclient = new DefaultHttpClient();
httppost = new HttpPost("your login link");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("param1", "param1_value"));
postParameters.add(new BasicNameValuePair("param2", "param2_value"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
第二种方式是:
String charset = "UTF-8";
String query = String.format("debug_data=%s&"
+ "client_auth_hash=%s&"
+ "timestamp=%s&"
+ "client_api_ver=%s&",
URLEncoder.encode("1", charset),
URLEncoder.encode(hash, charset),
URLEncoder.encode(timeStamp, charset),
URLEncoder.encode(clientApiVersion, charset));
System.setProperty("http.keepAlive", "false");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoOutput(true);
connection.setConnectTimeout(5000); //miliseconds
connection.setRequestMethod("POST");
connection.setRequestProperty("Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int status = ((HttpURLConnection) connection).getResponseCode();
Log.d("", "Status : " + status);
for (Entry<String, List<String>> header : connection
.getHeaderFields().entrySet()) {
Log.d("Headers",
"Headers : " + header.getKey() + "="
+ header.getValue());
}
InputStream response = new BufferedInputStream(connection.getInputStream());
int bytesRead = -1;
byte[] buffer = new byte[30 * 1024];
while ((bytesRead = response.read(buffer)) > 0) {
//read the response in pieces if it's needed
byte[] buffer2 = new byte[bytesRead];
}
connection.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
两种方式都正常运作。
答案 2 :(得分:0)
好吧,没有什么可以从战斗的方式 但这是我解决这个问题及其工作正常的方式
JSONObject json = new JSONObject();
json.put("intVal", -100);
json.put("enumVal", enumAsString);
StringEntity t = new StringEntity(json.toString(), "UTF-8");
httppost.setEntity(t);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");