除了wifi连接,我的Android应用程序不起作用

时间:2016-09-19 03:42:56

标签: android database mobile android-webservice wificonfiguration

我已在manifest

中授予用户权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

应用程序适用于wifi,但它不适用于移动数据连接。它没有崩溃。但它没有给出响应。

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

        try {
           URL = "http://" + Helper.getip(getApplicationContext(), LoginActivity.ipindex) + "/Android_Service/LoginService.asmx";
           URL2=  "http://"+Helper.getip(getApplicationContext(),LoginActivity.ipindex)+"/Android_Service/AndroidServices.asmx";
        final String imageurl="http://192.168.0.180/hrm/empimages100/" + LoginActivity.EmpNo + ".jpg";
        new LoadImage().execute(imageurl);

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        // mTitle = mDrawerTitle = getTitle();
        user = (ImageView) findViewById(R.id.avatar);

        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        Deviceid = tm.getDeviceId();

2 个答案:

答案 0 :(得分:2)

final String imageurl="http://192.168.0.180/hrm/empimages100/" 是指本地网络中的一个地址很难通过GPRS访问。 它不会返回任何错误,因为您在new LoadImage().execute(imageurl);部分中管理了一些异常。此字符串看起来像构造函数,但构建的对象不会去任何地方。

答案 1 :(得分:0)

你可以创建一个像这样的公共类来检查网络状态

public class NetworkStatus {
    private static NetworkStatus instance = new NetworkStatus();
    static Context context;
    ConnectivityManager connectivityManager;
    NetworkInfo wifiInfo, mobileInfo;
    boolean connected = false;

    public static NetworkStatus getInstance(Context ctx) {
        context = ctx;
        return instance;
    }
    public boolean isOnline(Context con) {
        try {
            connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            connected = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
            return connected;
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, "Check Connectivity Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            //Log.v("connectivity", e.toString());
        }
        return connected;
    }

    public boolean isConnectedToInternet(){
        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null){
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null){
                for (int i = 0; i < info.length; i++){
                    if (info[i].getState() == NetworkInfo.State.CONNECTED){
                        return true;
                    }
                }
            }
        }
        return false;
    }

}