我使用PhoneGap,因此我可以显示我的网页(如果可用),如果它不可用,我只使用资产中的自定义网页。我尝试了几个功能,但没有一个工作。请帮帮我,看看我做错了什么。
package com.test.project;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.setIntegerProperty("loadUrlTimeoutValue", 70000);
if (isSiteAvailable()) {
super.loadUrl("http://m.mywebsite.com", 10000);
Toast.makeText(getApplicationContext(), "Ima!", Toast.LENGTH_SHORT).show();
} else {
super.loadUrl("file:///android_asset/www/index.html", 10000);
Toast.makeText(getApplicationContext(), "nemaaa!", Toast.LENGTH_SHORT).show();
}
}
public boolean isSiteAvailable() {
try {
URL u = new URL("http://www.google.com");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
huc.setRequestProperty("Connection", "close");
huc.setConnectTimeout(2000); // Timeout 2 seconds.
int response=huc.getResponseCode();
if ((response >= 400) || (response < 200))
return false;
return true;
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("OOPSIE");
e.printStackTrace();
}
return false;
}
}
Manifest拥有互联网和网络的权限,config.xml允许我的网站网址,一切正常,除了检查连接。请告诉我是什么问题。 SDK版本是从8到17.我重新启动了eclipse并清除了几次。
我整天都在阅读主题,但我找不到问题。
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rs.mujen.nekretnine"
android:versionCode="1"
android:versionName="1.3" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:permission="android.permission.ACCESS_CHECKIN_PROPERTIES"
android:testOnly="false"
android:theme="@style/AppTheme" >
<activity
android:name="rs.mujen.nekretnine.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="org.apache.cordova.DroidGap"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
注释连接时间,因为网络延迟与时间无关
主要问题是带连接的urlconnection上的setRequestProperty() - &gt;关闭连接
public boolean isSiteAvailable() {
try {
URL u = new URL("http://www.google.com");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
//huc.setRequestProperty("Connection", "close");
//huc.setConnectTimeout(2000); // Timeout 2 seconds.
int response=huc.getResponseCode();
if ((response >= 400) || (response < 200))
return false;
return true;
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("OOPSIE");
e.printStackTrace();
}
return false;
}
希望这些代码适合您! :)