pvs*_*nik 3 post android curl outputstream httpurlconnection
我正在尝试使用HttpURLConnection执行post请求,但不知道如何正确执行.
我可以使用以下代码使用AndroidAsyncHttp客户端成功执行请求:
AsyncHttpClient httpClient = new AsyncHttpClient();
httpClient.addHeader("Content-type", "application/json");
httpClient.setUserAgent("GYUserAgentAndroid");
String jsonParamsString = "{\"key\":\"value\"}";
RequestParams requestParams = new RequestParams("request", jsonParamsString);
httpClient.post("<server url>", requestParams, jsonHttpResponseHandler);
Run Code Online (Sandbox Code Playgroud)
在桌面计算机上使用curl可以执行相同的请求:
curl -A "GYUserAgentAndroid" -d 'request={"key":"value"}' '<server url>'
Run Code Online (Sandbox Code Playgroud)
这两种方法都给了我服务器的预期响应.
现在我想使用HttpURLConnection做同样的请求.问题是我不知道如何正确地做到这一点.我尝试过这样的事情:
URL url = new URL("<server url>");
HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setRequestProperty("User-Agent", "GYUserAgentAndroid");
httpUrlConnection.setRequestProperty("Content-Type", "application/json");
httpUrlConnection.setUseCaches (false);
DataOutputStream outputStream = new DataOutputStream(httpUrlConnection.getOutputStream());
// what should I write here to output stream to post params to server ?
outputStream.flush();
outputStream.close();
// get response
InputStream responseStream = new BufferedInputStream(httpUrlConnection.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
stringBuilder.append(line);
}
responseStreamReader.close();
String response = stringBuilder.toString();
JSONObject jsonResponse = new JSONObject(response);
// the response is not I'm expecting
return jsonResponse;
Run Code Online (Sandbox Code Playgroud)
如何正确地将与AsyncHttpClient和curl的工作示例中的数据相同的数据写入HttpURLConnection输出流?
提前致谢.
public String getJson(String url,JSONObject params){
try {
URL _url = new URL(url);
HttpURLConnection urlConn =(HttpURLConnection)_url.openConnection();
urlConn.setRequestMethod(POSTMETHOD);
urlConn.setRequestProperty("Content-Type", "applicaiton/json; charset=utf-8");
urlConn.setRequestProperty("Accept", "applicaiton/json");
urlConn.setDoOutput(true);
urlConn.connect();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(urlConn.getOutputStream()));
writer.write(params.toString());
writer.flush();
writer.close();
if(urlConn.getResponseCode() == HttpURLConnection.HTTP_OK){
is = urlConn.getInputStream();// is is inputstream
} else {
is = urlConn.getErrorStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response = sb.toString();
Log.e("JSON", response);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return response ;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用以下内容来发布参数
outputStream.writeBytes(jsonParamsString);
outputStream.flush();
outputStream.close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17751 次 |
| 最近记录: |