嗨,我在使用http调用编写参数时遇到问题,我使用的是HttpUrlConnection,尤其是以下代码:
public static JSONObject sendError (String level, String message, String url2, String request, String response2, String headers, String method) throws IOException {
HttpURLConnection urlConnection = null;
URL url = new URL(MessageFormat.format("https://endpoint/api/{0}@{1}/log",slug, lang));
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream());
JSONObject param = new JSONObject();
try {
param.put("level", level);
param.put("message", message);
param.put("url", url2);
param.put("request", request);
param.put("response", response2);
param.put("headers", headers);
param.put("method", method);
} catch (JSONException e) {
e.printStackTrace();
}
outputStream.writeChars(param.toString());
outputStream.flush();
outputStream.close();
InputStream responseStream = new BufferedInputStream(urlConnection.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 = null;
try {
jsonResponse = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonResponse;
}
Web服务器写入请求,但所有值均为空,我想我编写参数的方式有问题,是否有解决此问题的建议?