Thursday, 19 September 2013

Simple application to read username, password from mysql and displaying the next activity after verification

Simple application to read username, password from mysql and displaying
the next activity after verification

" In android i'm trying to read username and password from mysql database
and displaying the next window using the 'intent' " There are two
activities, the main activity and the userpage.class the first one will
verify the username and password and using the 'intent' it will call the
second 'userpage'
import package com.example.loginform;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button login;
EditText username,password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
TextView tv;
List<NameValuePair> nameValuePairs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup();
}
private void setup() {
// TODO Auto-generated method stub
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button)findViewById(R.id.login);
status = (TextView)findViewById(R.id.tvstatus);
tv = (TextView)findViewById(R.id.editText1);
login.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.login:
login();
break;
}
}
private void login() {
// TODO Auto-generated method stub
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("localhost/android/index.php");
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new
BasicNameValuePair("username",username.getText().toString().trim()));
nameValuePairs.add(new
BasicNameValuePair("password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
ResponseHandler<String>responseHandler = new
BasicResponseHandler();
final String response =
httpclient.execute(httppost,responseHandler);
tv.setText(""+response);
if(response.equalsIgnoreCase("User Found"))
{
startActivity(new Intent(this,UserPage.class));
}
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(getBaseContext(),"Sorry error in the
connection!!",Toast.LENGTH_SHORT).show();
}
}
}
Error log:
09-19 12:59:11.367: E/Trace(2634): error opening trace file: No such file
or directory (2)

No comments:

Post a Comment