1

我开发了一个 android 应用程序,它连接到服务器并使用 url 从服务器访问 xml。我正在使用 HttpUrlConncetion 进行连接。我已经在一个 jar 文件中编写了这个连接代码,我的应用程序正在使用这个 jar 连接到服务器。当我使用模拟器时,这个应用程序工作正常,但是当我在我的手机上安装这个应用程序时,它会抛出异常。

我在清单文件中添加了互联网权限。

<uses-permission android:name="android.permission.INTERNET" />

例外是

Connection timed out 
org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:388)
org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:236)
org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:470)
java.net.Socket.connect(Socket.java:1055)
org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>           (HttpConnection.java:106)
org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>   (HttpConnection.java:79)org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:353)org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:120)org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection
(HttpURLConnectionImpl.java:316)org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection

(HttpURLConnectionImpl.java:298)
org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect

(HttpURLConnectionImpl.java:236)
com.tracking.jar.XmlDownLoader.openConnection(XmlDownLoader.java:46)
com.tracking.jar.XmlDownLoader.downLoadXml(XmlDownLoader.java:18)
com.tracking.jar.BusDetails.getProviderRouteBeanList

(BusDetails.java:179)
com.vehicletracking.GetProviderRouteBeanListAsyncTask.doInBackground

(GetProviderRouteBeanListAsyncTask.java:17)
com.vehicletracking.GetProviderRouteBeanListAsyncTask.doInBackground

(GetProviderRouteBeanListAsyncTask.java:1)
android.os.AsyncTask$2.call(AsyncTask.java:216)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:337)
java.util.concurrent.FutureTask.run(FutureTask.java:169)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1119)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:612)
java.lang.Thread.run(Thread.java:1052)

我的手机已植根,我安装了应用程序呼叫拦截器应用程序,可阻止来电和去电。

我的连接代码是:

public String downLoadXml(String urlString) throws Exception{
    urlString = urlString.replace(" ", "%20");
    String xfStr = "";
    HttpURLConnection httpConn = null;
    try{
        httpConn = openConnection(urlString);
        InputStream in = httpConn.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        String str =null;

        while((str = br.readLine())!= null){
            xfStr = xfStr + str;
        }
    }catch(Exception e){
        throw e;
    }
    finally{
        if(httpConn!=null){
            httpConn.disconnect();
        }
    }
    return xfStr;
}

private HttpURLConnection openConnection(String urlString) throws IOException{
    URL url  = new URL(urlString);
    HttpURLConnection httpConn = null;
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(false);
    httpConn.setRequestMethod("GET");
    httpConn.setConnectTimeout(60000);
    httpConn.connect();
    return httpConn;
}
4

2 回答 2

1

天才试试这个

try {
                HttpParams httpParams = new BasicHttpParams();
                int timeoutConnection = 3000;
                int socketTimeoutConnection = 5000;
                HttpConnectionParams.setConnectionTimeout(httpParams,
                        timeoutConnection);
                HttpConnectionParams.setSoTimeout(httpParams,
                        socketTimeoutConnection);
                DefaultHttpClient client = new DefaultHttpClient();
                client.setParams(httpParams);
                HttpGet httpGet = new HttpGet(postedScholarshipUrl);
                HttpResponse httpResponse = client.execute(httpGet);
                StatusLine statusLine = httpResponse.getStatusLine();
                 int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {
                    ResponseHandler<String> response = new BasicResponseHandler();
                    serverStr = client.execute(httpGet, response);
                    int myArray = new JSONArray(serverStr);
                    arrayLength = myArray.length();
                } else {
                    showDialog(connectionDown);
                }

            } catch (SocketTimeoutException bug) {
                Toast.makeText(getApplicationContext(), "Socket Timeout", Toast.LENGTH_LONG).show();
            } catch (ConnectTimeoutException bug) {
                Toast.makeText(getApplicationContext(), "Connection Timeout", Toast.LENGTH_LONG).show();
            } catch (Exception bug) {
                bug.printStackTrace();
            }
于 2014-06-17T05:32:20.263 回答
-1

确保手机已连接互联网。

您应该在执行 HTTP 请求之前检查网络连接。在 try catch 块中编写此代码。

于 2013-01-18T07:41:17.540 回答