2017-08-30 15 views
0

我只是一個登錄到另一個活動,代碼工作正常時,模擬器。要使其在線登錄代碼始終返回失敗消息。雖然用戶名和密碼是正確的。登錄使用volley libary在模擬器或本地主機上工作,但不在在線服務器

MainActivity.java

public class MainActivity extends AppCompatActivity { 

//Defining views 
private EditText editTextEmail; 
private EditText editTextPassword; 
private Button buttonLogin; 
private boolean loggedIn = false; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    editTextEmail = (EditText) findViewById(R.id.editTextEmail); 
    editTextPassword = (EditText) findViewById(R.id.editTextPassword); 

    buttonLogin = (Button) findViewById(R.id.buttonLogin); 

     buttonLogin.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Toast.makeText(MainActivity.this,"hello log me",Toast.LENGTH_SHORT).show(); 
      login(); 
     } 

    }); 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    //In onresume fetching value from sharedpreference 
    SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME,Context.MODE_PRIVATE); 

    //Fetching the boolean value form sharedpreferences 
    loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false); 

    //If we will get true 
    if(loggedIn){ 
     //We will start the Profile Activity 
     Intent intent = new Intent(MainActivity.this, ProfileActivity.class); 
     startActivity(intent); 
    } 
} 


private void login(){ 
    //Getting values from edit texts 
    final String email = editTextEmail.getText().toString().trim(); 
    final String password = editTextPassword.getText().toString().trim(); 
    Toast.makeText(MainActivity.this,email,Toast.LENGTH_SHORT).show(); 
    Toast.makeText(MainActivity.this,password,Toast.LENGTH_SHORT).show(); 


    //Creating a string request 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.LOGIN_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        // If we are getting success from server 
        if(response.trim().equals("success")){ 
         Intent intent = new Intent(MainActivity.this, ProfileActivity.class); 
         startActivity(intent); 
        }else{ 
         Toast.makeText(MainActivity.this,response,Toast.LENGTH_SHORT).show(); 
         Log.d("Else Error",response.toString()); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        //You can handle error here if you want 
        Toast.makeText(MainActivity.this, "hi there is "+error.toString(), Toast.LENGTH_LONG).show(); 
        Log.d("Hello",error.toString()); 
       } 
      }){ 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String,String> params = new HashMap<>(); 
      //Adding parameters to request 
      params.put(Config.KEY_EMAIL, email); 
      Log.d("Email :",email); 
      params.put(Config.KEY_PASSWORD, password); 
      Log.d("PASSWORD :",password); 

      //returning parameter 
      return params; 
     } 
    }; 

    //Adding the string request to the queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 
} 

Config.java

public class Config { 
//URL to our login.php file 

//public static final String LOGIN_URL = "http://10.0.2.2/android/login.php"; 
public static final String LOGIN_URL = "http://test.codeachi.com/Andi/android/login.php"; 

//Keys for email and password as defined in our $_POST['key'] in login.php 
public static final String KEY_EMAIL = "username"; 
public static final String KEY_PASSWORD = "password"; 

//If server response is equal to this that means login is successful 
public static final String LOGIN_SUCCESS = "success"; 

//Keys for Sharedpreferences 
//This would be the name of our shared preferences 
public static final String SHARED_PREF_NAME = "myloginapp"; 

//This would be used to store the email of current logged in user 
public static final String EMAIL_SHARED_PREF = "email"; 

//We will use this to store the boolean in sharedpreference to track user is loggedin or not 
public static final String LOGGEDIN_SHARED_PREF = "loggedin"; 

} 

的login.php

<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){ 
    $username = "naaz"; //$_POST['username']; 
    $password = "1234"; //$_POST['password']; 
    require_once('dbConnect.php'); 
    $sql ="SELECT * FROM volley WHERE username='$username' AND password='$password'"; 
    $result = mysqli_query($con,$sql); 
    $check = mysqli_fetch_array($result); 
    if(isset($check)){ 
    echo 'success'; 
    }else{ 
    echo 'failure'; 
    } 
    } 
    ?> 

代碼工作細時LOGIN_URL =「http://10.0.2.2/機器人/ login.php中「;但是當LOGIN_URL =「http://test.codeachi.com/Andi/android/login.php」; 其始終返回失敗消息。

+1

檢查Web服務.show錯誤信息 –

+0

爵士到底我的Web服務器上檢查? –

+1

請調試。使用'POSTMAN'。我不是SIR –

回答

相關問題