我开发了一个Android应用程序,它连接到服务器并使用url从服务器访问xml。我正在使用HttpUrlConncetion进行连接。我已经在jar文件中编写了这个连接代码,我的应用程序正在使用这个jar连接到服务器。当我使用模拟器时,这个应用程序正常工作但是当我在我的手机上安装这个应用程序时,它会抛出异常。
我在清单文件中添加了intenet权限。
<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;
}
答案 0 :(得分: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();
}
答案 1 :(得分:-2)
确保手机与互联网连接。
您应该在执行HTTP请求之前检查网络连接。在try catch块中写下这段代码。