Java 使用Android和HTTPSURLConnection发布数据
我意识到有很多关于如何使用Android和HttpURLConnection发布json数据的帖子。然而,不管我尝试了什么,我的贴身似乎永远不会到达/神奇地消失。我希望有人能建议我:Java 使用Android和HTTPSURLConnection发布数据,java,android,httpurlconnection,Java,Android,Httpurlconnection,我意识到有很多关于如何使用Android和HttpURLConnection发布json数据的帖子。然而,不管我尝试了什么,我的贴身似乎永远不会到达/神奇地消失。我希望有人能建议我: // Encrypt the post data String ciphertext = Crypt.encrypt(postData, encryptionKey); int postDataLength = ciphertext.getBytes(StandardCharsets.UT
// Encrypt the post data
String ciphertext = Crypt.encrypt(postData, encryptionKey);
int postDataLength = ciphertext.getBytes(StandardCharsets.UTF_8).length;
byte[] bCipertext = ciphertext.getBytes(StandardCharsets.UTF_8);
// Establish connection
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
// Set the request method
conn.setRequestMethod("POST");
// Set headers
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get(key).trim());
}
// Set content type - always application/json!
conn.setRequestProperty("Content-Type", "application/json");
// Set UTF-8 as the charset
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.addRequestProperty("Content-Type", "application/" + "POST");
// Don't cache data
conn.setUseCaches(false);
// Expect output
conn.setDoOutput(true);
if (!postData.isEmpty()) {
conn.setDoInput(true);
// Set content length
conn.setFixedLengthStreamingMode(postDataLength);
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(ciphertext);
out.close();
}
密文示例:
2.2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 mtq3uxpzafq==
编辑:
OKHttp仍然存在此问题:
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), ciphertext))
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.println(ciphertext);
String output = response.body().string();
System.out.println(output);
// Decrypt
String decryptedResponse = null;
try {
decryptedResponse = Crypt.decrypt(output.toString(), encryptionKey).trim();
} catch(Exception e) {
// If the response wasn't encrypted, then we will receive an exception
decryptedResponse = output.toString();
}
HttpResponse resp = new HttpResponse();
resp.setStatus(response.code());
接收请求的PHP本质上就是:
打印(邮政美元) > P>你应该考虑使用第三方库来处理HTTP请求。例如,okhttp大大减少了代码开销和错误数量:
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
此片段摘自:谢谢Moritz-不幸的是,问题仍然存在。使用OKHttp代码更新了答案您的请求在哪里过时?是否检查了服务器日志?问题是$\u POST正文为空。没有任何东西被发布。它基本上只是一个打印的帖子;显示php脚本“conn.setRequestProperty(“内容类型”、“应用程序/json”);”?您没有发送json。”conn.addRequestProperty(“内容类型”、“应用程序/”+“帖子”)???这是自相矛盾的。但它也不存在。最好尝试表单url编码。