Vikrant
Vikrant

Reputation: 444

Get data in java ( android studio ) via php

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 ?

Upvotes: 0

Views: 4586

Answers (1)

Harish Sharma
Harish Sharma

Reputation: 370

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.
        .....
     }

}

Upvotes: 3

Related Questions