Android-HttpsURLConnection不工作
我编写这个异步任务是为了通过Android手机上的HttpsURLConnection发送小块json。它不会在LogCat中抛出任何异常。但是我的Node.JS服务器(HTTPS!)没有收到任何请求。这个代码怎么了?谢谢你的回复Android-HttpsURLConnection不工作,android,sockets,https,httpsurlconnection,Android,Sockets,Https,Httpsurlconnection,我编写这个异步任务是为了通过Android手机上的HttpsURLConnection发送小块json。它不会在LogCat中抛出任何异常。但是我的Node.JS服务器(HTTPS!)没有收到任何请求。这个代码怎么了?谢谢你的回复 private class NetworkCommunication extends AsyncTask<String, Void, String> { @Override protected String doInBackgro
private class NetworkCommunication extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String paramUrl = params[0];
String paramRequest = params[1];
HttpsURLConnection connection = null;
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
try {
SSLContext sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
Log.d("meet", "NetworkCommunication::setDefaultSSLSocketFactory()");
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
Log.d("meet", "NetworkCommunication::setDefaultHostnameVerifier()");
URL url = new URL(paramUrl);
connection = (HttpsURLConnection) url.openConnection();
Log.d("meet", "NetworkCommunication::openConnection()");
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", String.valueOf(paramRequest.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
Log.d("meet", "NetworkCommunication::set...()");
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(paramRequest);
output.flush();
output.close();
Log.d("meet", "NetworkCommunication::DataOutputStream");
} catch (Exception e) {
Log.d("meet", "NetworkCommunication::Exception", e);
return null;
} finally {
if(connection != null)
connection.disconnect();
}
return null;
}
}
解决方案:
我必须得到回应,即使我不想要。我在“NetworkCommunication::DataOutputStream”之后添加了这段代码,它成功了
InputStream istream = connection.getInputStream();
BufferedReader input = new BufferedReader(new InputStreamReader(istream));
String line;
StringBuffer response = new StringBuffer();
while((line = input.readLine()) != null) {
response.append(line);
response.append('\n');
}
Log.d("meet", "NetworkCommunication::response=" + response.toString());
在我的情况下,清单中缺少互联网许可
<uses-permission android:name="android.permission.INTERNET" />
我认为您的问题更多地取决于您发布json请求的方式 您需要使用
setEntity
方法来设置HTTP请求的头
这里有一个关于如何做GET
和POST
为方便起见,我将相关代码粘贴在此处:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
我建议您尝试从请求中获取响应(在“NetworkCommunication::DataOutputStream”标志之后),并查看它得到了什么。似乎你的应用程序正在将数据上传到某个地方,问题是在哪里
另外,如果您不需要服务器响应,您可以尝试设置
connection.setDoInput(false)
任何日志?调试方面,您已经走了多远?您好njzk2,感谢您的快速响应。我不知道你的意思,但我在代码下附加了日志文件。请。我建议你尝试从请求中获取响应(在“NetworkCommunication::DataOutputStream”标志之后),并查看它得到了什么。看起来你的应用程序正在将数据上传到某个地方,问题是在哪里?它可以工作。我必须得到回应(无论我是否需要)。请发表您的评论,以便我可以接受它作为答案,我将更新我的问题代码。非常感谢。不,如果作者没有设置所需的权限,日志猫会给出异常。谢谢!但这不适用于HTTPS,不是吗?setEntity适用于HttpClient。使用的也是如此HttpURLConnection@raptor对不起,我没有仔细检查你的问题。我的回答与你的问题不符。
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
// Set HTTP parameters
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");