0

我使用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 具有 Internet 和网络权限,config.xml 允许我的网站 url,除了检查连接外一切正常。请告诉我有什么问题。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>

4

1 回答 1

0

将连接时间注释掉,因为网络延迟与时间无关

主要问题是带有连接的 urlconnection 上的 setRequestProperty() --> close 关闭连接

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;
}

希望这些代码对你有用!:)

于 2012-12-31T16:44:55.920 回答