2017-06-12 8 views
-1

Android Code ist hierFehler bei Anmeldung Erste webservice

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 CODE HIER

<?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); 
    } 
} 
?> 

Hier im Ungültige Benutzername und Passwort erhalten Sie mir helfen Benutzer Details aus JSON-Datei in Android zu bekommen wie man das lösen kann kann mir jemand sagen ?? Hier bekomme ich den ungültigen Benutzernamen und das Passwort, bitte helfen Sie mir, die Benutzerdetails von der JSON-Datei in Android zu erhalten, wie man dieses löst, kann mir jemand sagen?

+0

Ihre API ist eine GET-Anforderung, aber Sie sind eine POST-Anforderung in von Android zu schaffen . Auch die Schlüssel, die Sie an das PHP-Skript übergeben, sind falsch. Ihre MySQL-Abfrage macht auch keinen Sinn. –

+0

mir helfen, wie GET-Anfrage mit der gleichen PHP-Datei zu erstellen @KNeerajLal –

+0

'DefaultHttpClient' ist veraltet. Sehen Sie sich dies [link] (https://developer.android.com/training/volley/simple.html) an. –

Antwort

1

Einfach versuchen Sie,

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

sir kann diesen Code in meinem Android-Code bearbeiten bcoz ich bin neu in diesem Bereich pls helfen mir @ Harisudhan.A –

+0

Sie ersetzen nur die DoInBackground() von Ihrem Code zu meinem Code. es wird gut funktionieren. –

+0

geben Sie mir Fehler JSONParser @ Harisudhan.A –

0

Versuchen Sie, diese

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

Bearbeiten Sie Ihre 2-zeiliges

+0

Aber in der Datenbank ist der Spaltenname Benutzername und Passwort nur nicht myusername und meinpasswort @AanalSaha –

+0

ja, aber das ist nicht Ihr Spaltenname in $ _GET [ 'Ihr Schlüssel'] nicht Spaltenname @karan brahmaxatriya –

+0

bt, wenn ich nicht nichtupdate Login PHP-Datei gibt es eine andere Möglichkeit, Benutzername n Passwort? @AanalShah –

Verwandte Themen