2016-08-02 15 views
0

ログインしているアプリケーション、MySQL、PHP、アンドロイドスタジオを使用していたアンドロイドアプリケーションを実行していましたが、コンパイル時に問題が発生しました。私は、コードは以下の私のコードで問題が何であったかを見つけ出すことができなかったとPOSTメソッドで呼び出しを行うには、私は自分のコードにandroidログイン登録mysql php android app

package com.example.wandie.clinicapp; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.lang.reflect.Method; 
import java.util.HashMap; 
import java.util.Map; 

/** 
* Created by WANDIE on 8/1/2016. 
*/ 
public class LoginActivity extends Activity { 
    private static final String TAG = RegisterActivity.class.getSimpleName(); 
    private Button btnLogin; 
    private Button btnLinkToRegister; 
    private EditText inputEmail; 
    private EditText inputPassword; 
    private ProgressDialog pDialog; 
    private SessionManager session; 
    private SQLiteHandler db; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     inputEmail = (EditText) findViewById(R.id.email); 
     inputPassword = (EditText) findViewById(R.id.password); 
     btnLogin = (Button) findViewById(R.id.btnLogin); 
     btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen); 

     // Progress dialog 
     pDialog = new ProgressDialog(this); 
     pDialog.setCancelable(false); 

     // SQLite database handler 
     db = new SQLiteHandler(getApplicationContext()); 

     // Session manager 
     session = new SessionManager(getApplicationContext()); 

     // Check if user is already logged in or not 
     if (session.isLoggedIn()) { 
      // User is already logged in. Take him to main activity 
      Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
      startActivity(intent); 
      finish(); 
     } 

     // Login button Click Event 
     btnLogin.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       String email = inputEmail.getText().toString().trim(); 
       String password = inputPassword.getText().toString().trim(); 

       // Check for empty data in the form 
       if (!email.isEmpty() && !password.isEmpty()) { 
        // login user 
        checkLogin(email, password); 
       } else { 
        // Prompt user to enter credentials 
        Toast.makeText(getApplicationContext(), 
          "Please enter the credentials!", Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 

     }); 

     // Link to Register Screen 
     btnLinkToRegister.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(), 
         RegisterActivity.class); 
       startActivity(i); 
       finish(); 
      } 
     }); 

    } 

    /** 
    * function to verify login details in mysql db 
    * */ 
    private void checkLogin(final String email, final String password) { 
     // Tag used to cancel the request 
     String tag_string_req = "req_login"; 

     pDialog.setMessage("Logging in ..."); 
     showDialog(); 
//THIS IS THE PROBLEM I HAVE POST shows an error 
     StringRequest strReq = new StringRequest(Method.POST, 
       AppConfig.URL_LOGIN, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 
       Log.d(TAG, "Login Response: " + response.toString()); 
       hideDialog(); 

       try { 
        JSONObject jObj = new JSONObject(response); 
        boolean error = jObj.getBoolean("error"); 

        // Check for error node in json 
        if (!error) { 
         // user successfully logged in 
         // Create login session 
         session.setLogin(true); 

         // Now store the user in SQLite 
         String uid = jObj.getString("uid"); 

         JSONObject user = jObj.getJSONObject("user"); 
         String name = user.getString("name"); 
         String email = user.getString("email"); 
         String created_at = user 
           .getString("created_at"); 

         // Inserting row in users table 
         db.addUser(name, email, uid, created_at); 

         // Launch main activity 
         Intent intent = new Intent(LoginActivity.this, 
           MainActivity.class); 
         startActivity(intent); 
         finish(); 
        } else { 
         // Error in login. Get the error message 
         String errorMsg = jObj.getString("error_msg"); 
         Toast.makeText(getApplicationContext(), 
           errorMsg, Toast.LENGTH_LONG).show(); 
        } 
       } catch (JSONException e) { 
        // JSON error 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 

      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Login Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_LONG).show(); 
       hideDialog(); 
      } 
     }) { 

      @Override 
      protected Map<String, String> getParams() { 
       // Posting parameters to login url 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("email", email); 
       params.put("password", password); 

       return params; 
      } 

     }; 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 
    } 

    private void showDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hideDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 
+0

ここにlogcatを入れてください –

答えて

0

使用この方法を修正するために使用できるすべての可能な解決策を要求した

public static String httpPostRequest(Context context, String url, String data) { 
     String response = ""; 
     BufferedReader reader = null; 
     HttpURLConnection conn = null; 
     try { 
      Log.d("RequestManager", url + " "); 
      Log.e("data::", " " + data); 
      URL urlObj = new URL(url); 

      conn = (HttpURLConnection) urlObj.openConnection(); 
      conn.setRequestMethod("POST"); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 

      data += "&" + URLEncoder.encode("email", "UTF-8") + "=" 
        + URLEncoder.encode(email, "UTF-8"); 


      wr.write(data); 
      wr.flush(); 

      Log.d("post response code", conn.getResponseCode() + " "); 

      int responseCode = conn.getResponseCode(); 

      if (responseCode == 401) { 
       Intent i = new Intent(); 
       i.setAction(CUSTOM_INTENT); 
       context.sendBroadcast(i); 
      } 

      reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      response = sb.toString(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.d("Error", "error"); 
     } finally { 
      try { 
       reader.close(); 
       if (conn != null) { 
        conn.disconnect(); 
       } 
      } catch (Exception ex) { 
      } 
     } 
     Log.d("RESPONSE POST", response); 
     return response; 
    } 
+0

私はそのメソッドをs同じクラス – wandie

+0

はい。またはあなたはそれのための別のクラスファイルを作ることができます –

+0

いいえ私はクラスを作成させてくださいが、これは私のログの猫です – wandie

関連する問題