0

as the tittle indicates just want to get data from my php file & check whether entered email , password are of a registered user . If yes then redirect to Success page(TimelineActivity)

Hence had look on this :- How to get values from mysql database using php script in android

This my LoginBasicActivity.java

public class LoginBasicActivity extends AppCompatActivity {
public AutoCompleteTextView uemail;
public EditText upassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_basic);
   uemail = (AutoCompleteTextView) findViewById(R.id.emailBasic);
   upassword = (EditText) findViewById(R.id.passwordBasic)  ;
    Button buttonLog = (Button) findViewById(R.id.buttonLog);
    buttonLog.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String tem;
            String email = uemail.getText().toString();
            String result = null;
           String password = upassword.getText().toString() ;
           // HttpClientBuilder.create();

           // As  HttpClient client = new DefaultHttpClient(); is not used anymore
            HttpClient client = HttpClients.createDefault();

            tem = "http://www.example_domain.com/app_folder/verify-user.php?username="+email+"&password="+password;
            HttpGet request = new HttpGet(tem);
            try {
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader reader = new BufferedReader(isr);
                result = reader.readLine();
            } catch (Exception e) {
                e.printStackTrace();
            }

               if (result.equals("0") == true) {
                  Toast.makeText(getApplicationContext(), "Sorry try again", Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(getApplicationContext(), "Welcome Client", Toast.LENGTH_LONG).show();
                Intent myIntt = new Intent(view.getContext(), TimelineActivity.class);
                startActivityForResult(myIntt, 0);

              }
          }

      });
  }
 }

This is verify-user.php

 <?php

  mysql_connect("localhost","user_name","user_password") or die(mysql_error());
  mysql_select_db("db_name") or die(mysql_error());

  $username=$_REQUEST['username'];
  $password=$_REQUEST['password'];
  $SelectQuery="select * from table_name where C_Username='$username' and C_Password='$password'";
  $result=mysql_query($SelectQuery) or die(mysql_error());
  $count=mysql_num_rows($result);
  $row=mysql_fetch_array($result);
  if($count==0)
    echo "0";
  else
    echo $row['c_id'];
 ?>

After this i got some file duplication issue in build.gradle , so added this in build.gradle

 android {
 packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
   }
}`

Now , when Login button is pressed dialogbox says "Unfourtunately app_name has stopped "

Logcat :- https://www.dropbox.com/s/63cv806z4y8h9my/2015-11-10-05-01-44%5B1%5D.txt?dl=0

Im little bit new to Android-Java.

How to fix this issue ? Can someone explain correct syntax , where its going wrong ?

2
  • Always provide full logcat, we can not guess why your app crashes on you Commented Nov 10, 2015 at 8:50
  • Yes sure ! just a sec Commented Nov 10, 2015 at 8:51

1 Answer 1

3

You are making the network(api) request over main thread which is causing problem.Make the api hit from a thread or use the AsyncTask or Loader for it. Also check if you have added internet permission to your manifest file or not.if not than add the following line in your manifest file.

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

Edit: You can make a inner class like as follows in your activity and call it on button click using new LoginAsyncTask().execute();

public class LoginAsyncTask extends AsyncTask<Void, Void, String> {

 protected String doInBackground(Void... params) {
    //Run in background thread(can not access UI and not able to perform UI related operations here).
        //make your network hit here and return the result which is string in your case.
        //The onPostExecute Method is get called automatically after this method returns.
         ...

     }

      protected void onPreExecute() {
    //run on main thread(can access UI)
        //do some initialization here,if needed, before network hit.
      ...
     }

     protected void onPostExecute(String result) {
    //Method run on main thread,can access UI.result is the value returned by doInBackground method.
        //Put a check over result and perform the further operation accordingly.
        .....
     }

}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes , i have added <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> in my manifest
Can you show how AsyncTask is created , provide a code or some article about it ?
you can refer the google document for AsyncTask i.e. developer.android.com/intl/ko/reference/android/os/…
Thank you , achieved whatever was needed :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.