我是android开发的新手。 我正在开发一个android应用程序,其中我向Web服务器发送请求并解析json对象。 与服务器通信时,经常出现: 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) ();
(true);
("POST");
if(param != null){
(().length);
}
("Content-Type", "application/x-www-form-urlencoded");
out = new PrintWriter(());
if(param != null){
(param);
}
();
();
inStream = new Scanner(());
while(()){
response+=(());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
();
} catch (IOException e) {
// TODO Auto-generated catch block
();
} finally{
if(con != null){
();
}if(inStream != null){
();
}if(out != null){
();
();
}
}
}
Logcat:
03-25 10:55:32.613: W/(18868): : Connection
timed out
03-25 10:55:32.617: W/(18868):at (Native Method)
03-25 10:55:32.617: W/(18868):at
$(:357)
03-25 10:55:32.617: W/(18868):at
(:204)
03-25 10:55:32.617: W/(18868):at
(:437)
03-25 10:55:32.617: W/(18868):at (:1002)
03-25 10:55:32.621: W/(18868):at
.
(:75)
03-25 10:55:32.621: W/(18868): at
.
(:48)03-25 10:55:32.624: W/(18868):at
$
(:322)03-25 10:55:32.624: W/(18868):at
(:89)03-25 10:55:32.628: W/(18868):at
nection(:285)
03-25 10:55:32.628: W/(18868):at
ection(:267)
03-25 10:55:32.636: W/(18868):at
(:205)
03-25 10:55:32.636: W/(18868):at
tream(:614)
03-25 10:55:32.636: W/(18868):at
(:63)
03-25 10:55:32.636: W/(18868): at
$(:265)
03-25 10:55:32.640: W/(18868): at
$(:1)
03-25 10:55:32.640: W/(18868): at $
(:185)
03-25 10:55:32.640: W/(18868): at
$(:306)
03-25 10:55:32.640: W/(18868): at
(:138)
03-25 10:55:32.640: W/(18868): at
(:1088)
03-25 10:55:32.648: W/(18868): at
$(:581)
03-25 10:55:32.648: W/(18868): at (:1019)
03-25 10:55:32.652: E/JSON Parser(18868): Error parsing data :
End of input at character 0 of
谁能帮我找出解决方案? 提前致谢....