2017-06-12 75 views
-1

Android的代碼得到錯誤的登錄是這裏使用web服務

public class NewLogin extends ActionBarActivity { 

    private EditText editTextUserName; 
    private EditText editTextPassword; 

    public static final String USER_NAME = "USERNAME"; 

    String username; 
    String password; 

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

     editTextUserName = (EditText) findViewById(R.id.et_email); 
     editTextPassword = (EditText) findViewById(R.id.et_password); 
    } 

    public void invokeLogin(View view){ 
     username = editTextUserName.getText().toString(); 
     password = editTextPassword.getText().toString(); 

     login(username,password); 

    } 

    private void login(final String username, String password) { 

     class LoginAsync extends AsyncTask<String, Void, String>{ 

      private Dialog loadingDialog; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loadingDialog = ProgressDialog.show(NewLogin.this, "Please wait", "Loading..."); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       String uname = params[0]; 
       String pass = params[1]; 

       InputStream is = null; 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("myusername", uname)); 
       nameValuePairs.add(new BasicNameValuePair("mypassword", pass)); 
       String result = null; 

       try{ 
        HttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(
          "www.sample.com/home_webservice.php"); 
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpClient.execute(httpPost); 

        HttpEntity entity = response.getEntity(); 

        is = entity.getContent(); 

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result){ 
       String s = result.trim(); 
       loadingDialog.dismiss(); 
       if(s.equalsIgnoreCase("success")){ 
        Intent intent = new Intent(NewLogin.this, Sample.class); 
        /* intent.putExtra(USER_NAME, username); 
        finish();*/ 
        startActivity(intent); 
       }else { 
        Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 

     LoginAsync la = new LoginAsync(); 
     la.execute(username, password); 

    } 

/* 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    }*/ 
} 

JSON代碼是在這裏

<?php 
require '../db_connect.php'; 
if (isset($_GET['username']) && isset($_GET['password'])) 
{ 
    $myusername = $_GET['username']; 
    $mypassword = $_GET['password']; 
    $sql = "SELECT * FROM user_registration WHERE (username = '$myusername' or email = '$myusername' or phone = '$myusername')"; 
    $result = mysqli_query($con,$sql); 
    if(mysqli_num_rows($result)>0){ 

    $response["VerifiedMember"] = array(); 
    $row = mysqli_fetch_array($result); 
    $VerifiedMember = array(); 
     $id=$row['id']; 
     $phone=$row['phone']; 
     $reg_type=$row['register_type']; 
     $stored_salt = $row['salt']; 
     $stored_hash = $row['hashed_password']; 
     $check_pass = $stored_salt . $mypassword; 
     $check_hash = hash('sha512',$check_pass); 

     if($check_hash == $stored_hash){ 

      $VerifiedMember['user_id'] = $id; 
      $VerifiedMember['first_name']=$row['first_name']; 
      $VerifiedMember['phone']=$row['phone']; 
      array_push($response["VerifiedMember"], $VerifiedMember); 

     if(!empty($phone)&& $reg_type==1){ 

      $sql="select * from user_otps where user_id='".$id."'"; 
      $result = mysqli_query($con,$sql); 
      if(mysqli_num_rows($result)>0){ 
       $row = mysqli_fetch_array($result); 
       if($row['verified']==0) 
       { 
        //no product found 
         $response["success"] = 0; 
         $response["message"] = "failure"; 

        // echo no users JSON 
        echo json_encode($response); 

       } 
       else 
       { 
        //no product found 
         $response["success"] = 1; 
         $response["message"] = "success"; 

        // echo no users JSON 
        echo json_encode($response); 
       } 
      } 
     } 
     else{ 
        //no product found 
         $response["success"] = 1; 
         $response["message"] = "success"; 

        // echo no users JSON 
        echo json_encode($response); 
     } 
     //echo json_encode($response); 
     } 

     else{ 
        //no product found 
         $response["success"] = 0; 
         $response["message"] = "invalid"; 

        // echo no users JSON 
        echo json_encode($response); 
     } 
    } 
    else{ 
        //no product found 
         $response["success"] = 0; 
         $response["message"] = "invalid"; 

        // echo no users JSON 
        echo json_encode($response); 
    } 
} 
?> 

這裏即時得到無效的用戶名和密碼,請幫我從Android中的JSON文件中獲取用戶的詳細信息 如何解決這個問題誰能告訴我?? 在這裏我得到無效的用戶名和密碼,請幫我從json文件中獲取用戶詳細信息在android 如何解決這個任何人都可以告訴我??

+0

你的API是一個GET請求,但要創建從Android的POST請求。你傳遞給php腳本的鍵也是錯誤的。你的MySQL查詢沒有任何意義。 –

+0

幫助我如何使用相同的php文件創建GET請求@KNeerajLal –

+0

'DefaultHttpClient'已棄用。看看這個[link](https://developer.android.com/training/volley/simple.html)。 –

回答

1

只要你嘗試,

public class NewLogin extends ActionBarActivity { 

private EditText editTextUserName; 
private EditText editTextPassword; 

public static final String USER_NAME = "USERNAME"; 

String username; 
String password; 
String result; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_login); 

    editTextUserName = (EditText) findViewById(R.id.et_email); 
    editTextPassword = (EditText) findViewById(R.id.et_password); 
} 

public void invokeLogin(View view){ 
    new loginAccess().execute(); 
} 


class loginAccess extends AsyncTask<String, String, String> { 

    String access; 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Login..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 


    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     username = editTextUserName.getText().toString(); 
     password = editTextPassword.getText().toString(); 
     String url = "http://uat.ziplife.in/mobileapp/login_webservice.php"; 
     JSONObject json = null; 

     try { 
      params.add(new BasicNameValuePair("myusername", username)); 
      params.add(new BasicNameValuePair("mypassword", password)); 
      json = jsonParser.makeHttpRequest(url, "POST", params); 
      Log.d("TESS :: ", json.toString()); 
      String status = json.getString("sucess"); 
      Log.d("Success Response :: ", status); 


      if (status.equals("success")) { 


        Intent i = new Intent(NewLogin.this, Sample.class); 
        startActivity(i); 

      } 
     else if(json.getString("status").trim().equalsIgnoreCase("failed")) 
      { 


       Toast.makeText(NewLogin.this, "Please enter the correct details!!", Toast.LENGTH_LONG).show(); 

      } 


     } catch (Exception e1) { 
      // TODO Auto-generated catch block flag=1; 

      e1.printStackTrace(); 
     } 

     return null; 
    } 



    protected void onPostExecute(String file_url) { 
     pDialog.dismiss(); 



    } 
} 
+0

先生可以在我的android代碼bcoz編輯此代碼我在這方面新的請幫助我@ Harisudhan.A –

+0

您只需將代碼中的doInBackground()替換爲我的代碼即可。它會正常工作。 –

+0

給我錯誤JSONParser @ Harisudhan.A –

0

試試這個

<?php 
require '../db_connect.php'; 
if (isset($_POST['myusername']) && isset($_POST['mypassword'])) 
{ 
    $myusername = $_POST['myusername']; \\ change 
    $mypassword = $_POST['mypassword']; \\ change 

編輯您的2線

+0

但在數據庫中,列名僅爲用戶名和密碼而非myusername和mypassword @AanalSaha –

+0

是的,但這不是您在$ _GET中的列名稱[ '你的鑰匙']不是列名@karan brahmaxatriya –

+0

bt如果我不是不更新登錄php文件有任何其他方式來獲取用戶名n密碼? @AanalShah –