2016-06-20 4 views
0

は、無線LANを使用して実行されますが、モバイルデータとそれが働いていない:私のアプリはだけで実行モバイルデータ.itsで実行されていないエラーや例外が検出されないwifi.andコードの下

をここに私のコード

です
public class LoginActivity extends Activity { 

    Button btnlogin; 
    TextView txtforgot; 
    EditText edtpass,edtemail; 
    public static final String TAG="inetzetta"; 
    private int result; 
    boolean first_time; 
    Constants constants; 
    String stst,parent_email; 
    private String STATUS_CODE ; 
    AppPreferences appPreferences; 
    JSONObject jsonParam = new JSONObject(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     btnlogin= (Button) findViewById(R.id.btnlogin); 
     txtforgot= (TextView) findViewById(R.id.txtpass); 
     edtemail= (EditText) findViewById(R.id.edtemail); 
     edtpass= (EditText) findViewById(R.id.edtpass); 
     constants=new Constants(); 
     isConnected(LoginActivity.this); 
     appPreferences=new AppPreferences(this); 

     btnlogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if ((edtemail.getText().toString().trim().length() == 0) && (edtpass.getText().toString().trim().length() == 0)) { 
        edtemail.setError("Enter unique id"); 
        edtpass.setError("Enter Password"); 
       } 
       else if(edtemail.getText().toString().trim().length()==0) 
       { 
        edtemail.setError("email field is empty"); 
       } 
       else if(edtpass.getText().toString().trim().length()==0) 
       { 
        edtpass.setError("password field is empty"); 
       } 


       else { 

        stst = (appPreferences.getString("statusinlogin", "Default_Value")); 
        new LoginAsync().execute(edtemail.getText().toString(), edtpass.getText().toString()); 
        Log.d(TAG, "ckeck,check..." + stst); 


       } 
      } 
     }); 

     txtforgot.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(LoginActivity.this, ForgottPassword_Activity.class); 
       startActivity(intent); 

      } 
     }); 
    } 
/*Async for bacgroud task */ 
    public class LoginAsync extends AsyncTask<String,Void,Void>{ 
     HttpURLConnection conn= null; 
     //JSONObject jsonParam = new JSONObject(); 
     String jsonResp=""; 
     public String status; 
     String token; 
     String message; 
     String parentid; 
     String school_id; 
     @Override 
     protected Void doInBackground(String... params) { 


      String email = params[0]; 
      String password=params[1]; 
      Log.d(TAG, "Doinbackground"); 

      try { 
       URL url = new URL(constants.LOGIN +"unique_id=" + email + "&password=" + password); 

       // U String cookies = CookieManager.getgetCookie(url);RL url=new URL("http://192.168.1.100:8080/parent_portal/index.php/parent_login/login?parent_email=" + email + "&password=" + password); 
       conn =(HttpURLConnection) url.openConnection(); 

       Log.d(TAG, "url connection"); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Content-Type", "application/x- www-form-urlencoded"); 
       /* conn.setRequestProperty("Cookie","JSESSIONID=Sessionid");*/ 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 

       //conn.setInstanceFollowRedirects(false); 
       conn.connect(); 
       Log.d(TAG, "input connection"); 

       Uri.Builder builder=new Uri.Builder() 
         .appendQueryParameter("unique_id", email) 
         .appendQueryParameter("password", password); 


       String urlParameters=builder.build().getEncodedQuery(); 
       Log.d(TAG, "BeforeOutputStream"); 
       DataOutputStream wr=new DataOutputStream(conn.getOutputStream()); 
       wr.writeBytes(urlParameters); 
       wr.flush(); 
       wr.close(); 
       Log.d(TAG, "after dataoutput"); 

      } catch (UnsupportedEncodingException uEexc) { 

       Log.i("", 
         "Error Caught : USE : " 
           + uEexc); 
      } catch (ConnectTimeoutException cTexc) { 
       Log.i("", 
         "Error Caught in getconnection timeout : CTE : " 
           + cTexc); 
      } catch (IOException iOexc) { 
       Log.i("", 
         "Error Caught in io exception : IOE : " 
           + iOexc); 
      } catch (Exception exc) { 
       Log.i("", 
         "Error Caught in exception : E : " 
           + exc); 
      } 
      try { 

       Log.d(TAG,"before bufferedreader"); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
       Log.d(TAG,"after bufferedreader"); 
       StringBuilder sb = new StringBuilder(); 

       String line ; 
       while ((line = reader.readLine()) != null) { 

        sb.append(line + "\n"); 
       } 
       reader.close(); 

       jsonResp = sb.toString(); 
       Log.d(TAG, "result shows=" + jsonResp); 


       jsonParam=new JSONObject(jsonResp); 

      }catch(Exception exc){ 
       Log.i(TAG, 
         "Error in Api... : " 
           + exc); 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 

     try { 

      status = jsonParam.getString("status"); 

      if (status.equals("success")) { 
       JSONArray newJobj = jsonParam.getJSONArray("data"); 
       for (int i = 0; i < newJobj.length(); i++) { 
      JSONObject newjson = newJobj.getJSONObject(i); 

      token = newjson.getString("Token"); 
      parentid = newjson.getString("parent_id"); 
      school_id = newjson.getString("school_id"); 
      parent_email = newjson.getString("Parent_Email"); 


      Log.d(TAG, "result shows=" + result); 
      Log.d(TAG, "message " + message); 
      Log.d(TAG, "token is " + token); 
      status = jsonParam.getString("status"); 
      Log.d(TAG, "Printing status in async." + status); 
      Log.d(TAG, "parend id from api" + parentid); 
      Log.d(TAG, "parent email from async" + parent_email); 


      appPreferences.putString("NameOfShared", token); 
      appPreferences.putString("statuslogin", status); 
      appPreferences.putString("parent_id", parentid); 
      appPreferences.putString("parent_email", parent_email); 
      appPreferences.putString("school_id", school_id); 
      Log.d(TAG, "value of token" + token); 
     } 
       Toast.makeText(LoginActivity.this, "Login successfull", Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
       startActivity(intent); 

      } 

      else if(status.equals("failure")){ 
       String str=jsonParam.getString("data"); 
       Toast.makeText(LoginActivity.this,str,Toast.LENGTH_SHORT).show(); 


     }} 
       catch(Exception exc){ 
        Log.i(TAG, 
       "Error in Api... : " 
        + exc); 

       } 

     } 



    } 


/*Mobile data connectivity*/ 
    public boolean isConnected(Context context) { 
     boolean haveConnectedWifi = false; 
     boolean haveConnectedMobile = false; 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
     for (NetworkInfo ni : netInfo) { 
      if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
       if (ni.isConnected()) 
        haveConnectedWifi = true; 
      Log.d(TAG,"my log for connectivity"+haveConnectedWifi); 
      if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
       if (ni.isConnected()) 
        haveConnectedMobile = true; 
      Log.d(TAG,"my log for connectivity"+haveConnectedMobile); 
     } 
     return haveConnectedWifi || haveConnectedMobile; 

    } 

    private void showDialogMobile() 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Connect to mobile data or wifi") 
       .setCancelable(false) 
       .setPositiveButton("Connect to internet", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0); 
         /* Intent intent = new Intent(Intent.ACTION_MAIN); 
         intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting"); 
         startActivity(intent);*/ 
        } 
       }) 
       .setNegativeButton("Quit", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
    @Override 
    public void onBackPressed() { 

       onBackPressedLogin(); 

     // super.onBackPressed(); 
} 
    public void onBackPressedLogin() { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       LoginActivity.this); 

     alertDialogBuilder.setTitle("Exit"); 

     alertDialogBuilder 
       .setMessage("Do you really want to exit?") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 

         finish(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         dialog.cancel(); 
        } 
       }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 

     alertDialog.show(); 
    } 
} 
+0

他のデバイスをチェックインしましたか? –

+2

こんにちは。 *** [MCVE](http://stackoverflow.com/help/mcve)*** –

+0

あなたにはどんなエラーがありますか? –

答えて

0

コードの下に試してみてください

/** 
    * Function for check network connection 
    * @return : network connection status 
    */ 
    private boolean networkAvailable() { 
     ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
     if (networkInfo != null) 
      return connectivityManager.getActiveNetworkInfo().isConnected(); 
     else 
      return false; 
    } 
0

、これを試してみてください。

public class LoginActivity extends Activity { 

    Button btnlogin; 
    TextView txtforgot; 
    EditText edtpass,edtemail; 
    public static final String TAG="inetzetta"; 
    private int result; 
    boolean first_time; 
    Constants constants; 
    String stst,parent_email; 
    private String STATUS_CODE ; 
    AppPreferences appPreferences; 
    JSONObject jsonParam = new JSONObject(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     btnlogin= (Button) findViewById(R.id.btnlogin); 
     txtforgot= (TextView) findViewById(R.id.txtpass); 
     edtemail= (EditText) findViewById(R.id.edtemail); 
     edtpass= (EditText) findViewById(R.id.edtpass); 
     constants=new Constants(); 

     appPreferences=new AppPreferences(this); 

     btnlogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if ((edtemail.getText().toString().trim().length() == 0) && (edtpass.getText().toString().trim().length() == 0)) { 
        edtemail.setError("Enter unique id"); 
        edtpass.setError("Enter Password"); 
       } 
       else if(edtemail.getText().toString().trim().length()==0) 
       { 
        edtemail.setError("email field is empty"); 
       } 
       else if(edtpass.getText().toString().trim().length()==0) 
       { 
        edtpass.setError("password field is empty"); 
       } 
       else { 

        if(isConnected(LoginActivity.this)) { 
         stst = (appPreferences.getString("statusinlogin", "Default_Value")); 
         new LoginAsync().execute(edtemail.getText().toString(), edtpass.getText().toString()); 
         Log.d(TAG, "ckeck,check..." + stst); 
        } else { 
         showDialogMobile(); 
        } 
       } 
      } 
     }); 

     txtforgot.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(LoginActivity.this, ForgottPassword_Activity.class); 
       startActivity(intent); 

      } 
     }); 
    } 
/*Async for bacgroud task */ 
    public class LoginAsync extends AsyncTask<String,Void,Void>{ 
     HttpURLConnection conn= null; 
     //JSONObject jsonParam = new JSONObject(); 
     String jsonResp=""; 
     public String status; 
     String token; 
     String message; 
     String parentid; 
     String school_id; 
     @Override 
     protected Void doInBackground(String... params) { 


      String email = params[0]; 
      String password=params[1]; 
      Log.d(TAG, "Doinbackground"); 

      try { 
       URL url = new URL(constants.LOGIN +"unique_id=" + email + "&password=" + password); 

       // U String cookies = CookieManager.getgetCookie(url);RL url=new URL("http://192.168.1.100:8080/parent_portal/index.php/parent_login/login?parent_email=" + email + "&password=" + password); 
       conn =(HttpURLConnection) url.openConnection(); 

       Log.d(TAG, "url connection"); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Content-Type", "application/x- www-form-urlencoded"); 
       /* conn.setRequestProperty("Cookie","JSESSIONID=Sessionid");*/ 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 

       //conn.setInstanceFollowRedirects(false); 
       conn.connect(); 
       Log.d(TAG, "input connection"); 

       Uri.Builder builder=new Uri.Builder() 
         .appendQueryParameter("unique_id", email) 
         .appendQueryParameter("password", password); 


       String urlParameters=builder.build().getEncodedQuery(); 
       Log.d(TAG, "BeforeOutputStream"); 
       DataOutputStream wr=new DataOutputStream(conn.getOutputStream()); 
       wr.writeBytes(urlParameters); 
       wr.flush(); 
       wr.close(); 
       Log.d(TAG, "after dataoutput"); 

      } catch (UnsupportedEncodingException uEexc) { 

       Log.i("", 
         "Error Caught : USE : " 
           + uEexc); 
      } catch (ConnectTimeoutException cTexc) { 
       Log.i("", 
         "Error Caught in getconnection timeout : CTE : " 
           + cTexc); 
      } catch (IOException iOexc) { 
       Log.i("", 
         "Error Caught in io exception : IOE : " 
           + iOexc); 
      } catch (Exception exc) { 
       Log.i("", 
         "Error Caught in exception : E : " 
           + exc); 
      } 
      try { 

       Log.d(TAG,"before bufferedreader"); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 
       Log.d(TAG,"after bufferedreader"); 
       StringBuilder sb = new StringBuilder(); 

       String line ; 
       while ((line = reader.readLine()) != null) { 

        sb.append(line + "\n"); 
       } 
       reader.close(); 

       jsonResp = sb.toString(); 
       Log.d(TAG, "result shows=" + jsonResp); 


       jsonParam=new JSONObject(jsonResp); 

      }catch(Exception exc){ 
       Log.i(TAG, 
         "Error in Api... : " 
           + exc); 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 

     try { 

      status = jsonParam.getString("status"); 

      if (status.equals("success")) { 
       JSONArray newJobj = jsonParam.getJSONArray("data"); 
       for (int i = 0; i < newJobj.length(); i++) { 
      JSONObject newjson = newJobj.getJSONObject(i); 

      token = newjson.getString("Token"); 
      parentid = newjson.getString("parent_id"); 
      school_id = newjson.getString("school_id"); 
      parent_email = newjson.getString("Parent_Email"); 


      Log.d(TAG, "result shows=" + result); 
      Log.d(TAG, "message " + message); 
      Log.d(TAG, "token is " + token); 
      status = jsonParam.getString("status"); 
      Log.d(TAG, "Printing status in async." + status); 
      Log.d(TAG, "parend id from api" + parentid); 
      Log.d(TAG, "parent email from async" + parent_email); 


      appPreferences.putString("NameOfShared", token); 
      appPreferences.putString("statuslogin", status); 
      appPreferences.putString("parent_id", parentid); 
      appPreferences.putString("parent_email", parent_email); 
      appPreferences.putString("school_id", school_id); 
      Log.d(TAG, "value of token" + token); 
     } 
       Toast.makeText(LoginActivity.this, "Login successfull", Toast.LENGTH_SHORT).show(); 
       Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
       startActivity(intent); 

      } 

      else if(status.equals("failure")){ 
       String str=jsonParam.getString("data"); 
       Toast.makeText(LoginActivity.this,str,Toast.LENGTH_SHORT).show(); 


     }} 
       catch(Exception exc){ 
        Log.i(TAG, 
       "Error in Api... : " 
        + exc); 

       } 

     } 



    } 


/*Mobile data connectivity*/ 
    public boolean isConnected(Context context) { 
    boolean haveNetworkConnected = false; 
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 

    NetworkInfo ni = cm.getActiveNetworkInfo(); 
    if (ni != null) { 
     haveNetworkConnected = ni.isConnected(); 
    } 
    return haveNetworkConnected; 
} 

    private void showDialogMobile() 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Connect to mobile data or wifi") 
       .setCancelable(false) 
       .setPositiveButton("Connect to internet", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0); 
         /* Intent intent = new Intent(Intent.ACTION_MAIN); 
         intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting"); 
         startActivity(intent);*/ 
        } 
       }) 
       .setNegativeButton("Quit", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
    @Override 
    public void onBackPressed() { 

       onBackPressedLogin(); 

     // super.onBackPressed(); 
    } 
    public void onBackPressedLogin() { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
       LoginActivity.this); 

     alertDialogBuilder.setTitle("Exit"); 

     alertDialogBuilder 
       .setMessage("Do you really want to exit?") 
       .setCancelable(false) 
       .setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 

         finish(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         dialog.cancel(); 
        } 
       }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 

     alertDialog.show(); 
    } 
} 
関連する問題