-3

" 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)
pnuts
  • 58,317
  • 11
  • 87
  • 139
atul
  • 5
  • 1
  • 7
  • 2
    and whats your question? – silvia_aut Sep 19 '13 at 08:03
  • oh!!, the problem is that the connection between the main activity and the .php file is not getting established.And main is throwing the exception. – atul Sep 19 '13 at 08:05
  • Which exception? The Error log you posted is unrelated. – Henry Sep 19 '13 at 08:07
  • 09-19 12:58:50.700: W/ActivityManager(1010): Force removing ActivityRecord{b3b57608 com.example.loginform/.MainActivity}: app died, no saved state , its an error actually – atul Sep 19 '13 at 08:13
  • 09-19 13:51:16.587: W/System.err(5799): android.os.NetworkOnMainThreadException 09-19 13:51:16.616: W/System.err(5799): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) is the exception occurs – atul Sep 19 '13 at 09:28

1 Answers1

0

The problem is with your code.

 httppost = new HttpPost("localhost/android/index.php");

You can't access this link. You must give your IPAddress and port number along with this URL. and also your testing device must be in the same network to access this URL. It will be like this

httppost = new HttpPost("http://10.0.2.2:8080/android/index.php"); 

Please refer this Connect an Android Device To a Web Service on Local Host

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 :
        new LoginOperation().execute();
            break;

    }

}
class LoginOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        return login();
    }

    @Override
    protected void onPostExecute(String result) {
        tv.setText("" + result);
        if (result.equalsIgnoreCase("User Found")) {

            startActivity(new Intent(this, UserPage.class));
        }
    }

    @Override
    protected void onPreExecute() {
    }
}

private String 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);

        return response;
    } catch (Exception ex) {
        return "";
    }
}
 }
Community
  • 1
  • 1
diordna
  • 550
  • 5
  • 14
  • made those changes but now the following exception is printed: 09-19 13:51:16.587: W/System.err(5799): android.os.NetworkOnMainThreadException 09-19 13:51:16.616: W/System.err(5799): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) – atul Sep 19 '13 at 09:28
  • When you call a web service it should not be in UI thread. So you must implement login() method inside a Asynchronous task. Please refer my answer i have updated it. – diordna Sep 19 '13 at 10:25