2017-02-11 22 views
0

私のアプリはlogin.phpによってリモートデータベースにメールとパスワードのペアが存在するかどうかをチェックする必要があります。
私は、外部ページとしてのログインからAsyncTaskを拡張する内部クラスを使用して、Android Studioログインアクティビティを実装しました。Android Studioログインの実装方法AsyncTask外部データベースを拡張するクラスでのアクティビティ

private BackgroundWorker backgroundWork= null; 
    private void attemptLogin() { 
     if (backgroundWorker != null) { 
      return; 
     } 
     mEmailView.setError(null); 
     mPasswordView.setError(null); 
     String email = mEmailView.getText().toString(); 
     String password = mPasswordView.getText().toString(); 

     boolean cancel = false; 
     View focusView = null; 
     if (TextUtils.isEmpty(password)) { 
      mPasswordView.setError(getString(R.string.error_field_required)); 
      focusView = mPasswordView; 
      cancel = true; 
     } else if (!isPasswordValid(password)) { 
      mPasswordView.setError(getString(R.string.error_invalid_email)); 
      focusView = mPasswordView; 
      cancel = true; 
     } 
     if (TextUtils.isEmpty(email)) { 
      mEmailView.setError(getString(R.string.error_field_required)); 
      focusView = mEmailView; 
      cancel = true; 
     } else if (!isEmailValid(email)) { 
      mEmailView.setError(getString(R.string.error_invalid_email)); 
      focusView = mEmailView; 
      cancel = true; 
     } 
     if (cancel) { 
      focusView.requestFocus(); 
     } else { 
      showProgress(true); 
      String type = "login"; 
      BackgroundWorker backgroundWorker = new BackgroundWorker(type, email, password); 
      backgroundWorker.execute((Void) null); 
     } 
    } 

そしてこのAsynctask延びインナークラス:外側のクラスの主な部分は、このある

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 
    Process: dk.hydraulik, PID: 3894 
    java.lang.RuntimeException: An error occurred while executing doInBackground() 
    at android.os.AsyncTask$3.done(AsyncTask.java:309) 
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 
     Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?) 
    at java.net.InetAddress.lookupHostByName(InetAddress.java:464) 
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
    at java.net.InetAddress.getAllByName(InetAddress.java:215) 
    at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29) 
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188) 
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157) 
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100) 
    at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357) 
    at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340) 
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) 
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114) 
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245) 
    at dk.hydraulik.LoginActivity$BackgroundWorker.doInBackground(LoginActivity.java:204) 
    at dk.hydraulik.LoginActivity$BackgroundWorker.doInBackground(LoginActivity.java:187) 
    at android.os.AsyncTask$2.call(AsyncTask.java:295) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)  
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  
    at java.lang.Thread.run(Thread.java:818)  
         Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated 
Edit:I added the internet permission, and now the above error is gone but it still is not working with the correct email and password.

public class BackgroundWorker extends AsyncTask<Void, Void, String> { 
      private final String mEmail; 
      private final String mPassword; 
      private final String mtype; 
      public BackgroundWorker(String type,String email, String pass) { 
       mEmail=email;mtype=type;mPassword=pass; 
      } 
      @Override 
      protected String doInBackground(Void... params) { 
       String login_url = "http://www.example.com/login.php"; 
// Returns a string of "1" if it is correct email and password 
       if (mtype.equals("login")) { 
        try { 
         URL url = new URL(login_url); 
         HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
         httpURLConnection.setRequestMethod("POST"); 
         httpURLConnection.setDoOutput(true); 
         httpURLConnection.setDoInput(true); 
         OutputStream outputStream = httpURLConnection.getOutputStream(); 
         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
         String post_data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(mEmail, "UTF-8") + "&" 
           + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(mPassword, "UTF-8"); 
         bufferedWriter.write(post_data); 
         bufferedWriter.flush(); 
         bufferedWriter.close(); 
         outputStream.close(); 
         InputStream inputStream = httpURLConnection.getInputStream(); 
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
         String result = ""; 
         String line = ""; 
         while ((line = bufferedReader.readLine()) != null) { 
          result += line; 
         } 
         bufferedReader.close(); 
         inputStream.close(); 
         httpURLConnection.disconnect(); 
         return result; 
        } catch (MalformedURLException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       return "0"; 
      } 
      @Override 
      protected void onPostExecute(String result) { 
    //   backgroundWork = null; 
       showProgress(false); 
       if (result == "1") { 
        Intent i = new Intent(getApplicationContext(), MainActivity.class); 
        startActivity(i); 
       } else { 
        mPasswordView.setError(getString(R.string.error_incorrect_password)); 
        mPasswordView.requestFocus(); 
       } 
      } 

      @Override 
      protected void onCancelled() { 
       showProgress(false); 
      } 
     } 
However I managed to implement it with a simple Login Activity and a separate public class in a different project successfully.

、スタックトレースをこのあります

ここにこのエラーが表示されます:

$ adb shell am start -n "dk.hydraulik/dk.hydraulik.LoginActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
Connected to process 3640 on device emulator-5554 
W/System: ClassLoader referenced unknown path: /data/app/dk.hydraulik-1/lib/x86_64 
I/InstantRun: Instant Run Runtime started. Android package is dk.hydraulik, real application class is null. 
W/System: ClassLoader referenced unknown path: /data/app/dk.hydraulik-1/lib/x86_64 
W/art: Verification of android.support.v7.view.ActionMode android.support.v7.app.AppCompatDelegateImplV7.startSupportActionModeFromWindow(android.support.v7.view.ActionMode$Callback) took 205.213ms 
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
I/OpenGLRenderer: Initialized EGL, version 1.4 
D/gralloc_ranchu: Emulator without host-side GPU emulation detected. 
I/Choreographer: Skipped 64 frames! The application may be doing too much work on its main thread. 
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread. 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
I/Choreographer: Skipped 55 frames! The application may be doing too much work on its main thread. 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 

何か助けていただければ幸いです。コメントをして回答してください。必要に応じて外部URLコンテンツを変更することができます。

+0

インターネットのアクセス許可がありません。.. –

答えて

3

あなたはインターネット許可

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

編集このようなあなたのAndroidManifest.xml含まれている必要があります最初のネットワーク接続が

private boolean isNetWorkAvailable() { 
    boolean haveConnectedWifi = false; 
    boolean haveConnectedMobile = false; 

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 
    } 
    return haveConnectedWifi || haveConnectedMobile; 
} 

使用HTTPリクエストを呼び出す前に利用可能であるかどうかを

<manifest xlmns:android...> 
... 
<uses-permission android:name="android.permission.INTERNET" /> 
<application ... 
</manifest> 
+0

はい、you'r右最初の原因について。 – Oskar

1

チェックそれは:

if(isNetWorkAvailable()){ 
    BackgroundWorker backgroundWorker = new BackgroundWorker(type, email, password); 
    backgroundWorker.execute((Void) null); 

    } else{ 
    //show a toast message that network not available 
    } 

はまた、マニフェストにパーミッションを設定する必要があります。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
+0

getAllNetworkInfo()は推奨されていません! – Oskar

関連する問題