我是android开发的新手。 我正在开发一个android应用程序,其中我向Web服务器发送请求并解析json对象。 与服务器通信时,经常出现java.net.SocketTimeoutException: Connection timed out异常。 有时它会完美地工作而不会出现任何问题。我知道这个问题已经问过很多次了。 但是我仍然没有得到令人满意的解决方案。 我在下面发布了我的logcat和应用程序服务器通信代码。
public JSONObject RequestWithHttpUrlConn(String _url, String param){
HttpURLConnection con = null;
URL url;
String response = "";
Scanner inStream = null;
PrintWriter out = null;
try {
url = new URL(_url);
con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
if(param != null){
con.setFixedLengthStreamingMode(param.getBytes().length);
}
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
out = new PrintWriter(con.getOutputStream(