相关疑难解决方法(0)

获取java.net.SocketTimeoutException:连接在android中超时

我对android开发比较陌生.我正在开发一个Android应用程序,我正在向Web服务器发送请求并解析json对象.我经常java.net.SocketTimeoutException: Connection timed out在与服务器通信时遇到异常.有时它会毫无问题地完美运行.我知道这个问题在很多时候都被提出过.但我仍然没有得到任何满意的解决方案来解决这个问题.我正在下面发布我的logcat和app-server通信代码.

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());
        if(param != null){
            out.print(param);
        }
        out.flush();
        out.close();
        inStream = new Scanner(con.getInputStream());

        while(inStream.hasNextLine()){
            response+=(inStream.nextLine());
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch …
Run Code Online (Sandbox Code Playgroud)

java android httpurlconnection java-io

68
推荐指数
3
解决办法
25万
查看次数

HttpURLConnection在Android 2.x中运行良好但在4.1中没有运行:没有发现身份验证挑战

我有一些典型的代码使用HttpURLConnection来获取带有URL的文件.他们在android 1.x和2.x中运行良好.但在Android 4.1中失败了!

我在网上搜索但发现的信息很少.有人请帮忙调查这个问题吗?

private String mURLStr; 
private HttpURLConnection mHttpConnection;

...

url = new URL(mURLStr);

...

mHttpConnection = (HttpURLConnection) url.openConnection();
mHttpConnection.setDoOutput(true);
mHttpConnection.setRequestMethod("GET");

...

InputStream is = mHttpConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)

getInputStream方法抛出异常:

08-01 15:56:48.856: W/System.err(13613): java.io.IOException: No authentication challenges found
08-01 15:56:48.856: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
08-01 15:56:48.866: W/System.err(13613):      at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
...
Run Code Online (Sandbox Code Playgroud)

authentication android httpurlconnection

30
推荐指数
3
解决办法
2万
查看次数