0

I am trying to create a simple login interface in android, but when I try to run the connection error has been occurred. I don't know , why this happens?

Here is my code:

public class MainActivity extends Activity{

    String username,password;

    HttpClient httpclient;
    HttpPost httppost;
    HttpResponse response;
    HttpEntity httpentity;
    ArrayList<NameValuePair> namevaluepairs;


    EditText etUser,etPass;
    Button bLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etUser=(EditText) findViewById(R.id.etUser);
        etPass=(EditText) findViewById(R.id.etPass);

        bLogin=(Button) findViewById(R.id.bSubmit);
        bLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                httpclient=new DefaultHttpClient();
                httppost=new HttpPost("http://192.168.1.107/php_project/checking.php");

                username=etUser.getText().toString();
                password=etPass.getText().toString();

                try {

                    namevaluepairs=new ArrayList<NameValuePair>();

                    namevaluepairs.add(new BasicNameValuePair(username, username));
                    namevaluepairs.add(new BasicNameValuePair("password", password));

                    httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
                    response=httpclient.execute(httppost);
                    if(response.getStatusLine().getStatusCode()==200){

                        httpentity=response.getEntity();

                        if(httpentity!=null){
                            InputStream inputstream=httpentity.getContent();

                            JSONObject jsonResponse=new JSONObject(convertStreamToString(inputstream));

                            String retUser=jsonResponse.getString("user");
                            String retPass=jsonResponse.getString("pass");

                            if(username.equals(retUser) && password.equals(retPass)){
                                SharedPreferences sp=getSharedPreferences("logindetails", 0);
                                SharedPreferences.Editor spedit=sp.edit();

                                spedit.putString("user", username);
                                spedit.putString("pass", password);

                                spedit.commit();

                                Toast.makeText(getApplicationContext(), "SUCCESS !", Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(getApplicationContext(), "Inavalid Login details", Toast.LENGTH_LONG).show();
                            }
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();

                    Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_LONG).show();
                }                   
            }
        });


    }

    private static String convertStreamToString(InputStream is){

        BufferedReader reader=new BufferedReader(new InputStreamReader(is));
        StringBuilder sb=new StringBuilder();

        String line=null;

        try {
            while((line=reader.readLine())!=null){
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                is.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        return sb.toString();


    }
}

can any one help me to solve this problem.. When I run it in the emulator , It gives me the Toast message i.e "Connection error"...

I simply can't understand , why I cant i make it cuccesful login...???

Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
Archana Sarang
  • 101
  • 1
  • 9
  • Logcat: 09-06 05:35:48.794: W/System.err(1564): android.os.NetworkOnMainThreadException 09-06 05:35:48.804: W/System.err(1564): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) – Archana Sarang Sep 06 '14 at 09:39
  • use cannot perform internet related Task in UI Thread.. use AsynTask for call PHP Page http://developer.android.com/reference/android/os/AsyncTask.html – Manjeet Brar Sep 06 '14 at 12:56

2 Answers2

1
 Check whether you have given Network Connection permission in manifest.

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Shadow
  • 6,864
  • 6
  • 44
  • 93
0

Please add what the logcat when asking questions like this.

Looking at the IP my guess is that you want to use 10.0.2.2.

Read more here: http://developer.android.com/tools/devices/emulator.html#emulatornetworking

Heinrisch
  • 5,835
  • 4
  • 33
  • 43
  • Logcat-----09-06 05:35:48.794: W/System.err(1564): android.os.NetworkOnMainThreadException 09-06 05:35:48.804: W/System.err(1564): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) – Archana Sarang Sep 06 '14 at 09:41
  • So you need to do the network request in a separate thread. Check this answer: http://stackoverflow.com/questions/3505930/make-an-http-request-with-android – Heinrisch Sep 06 '14 at 11:24
  • Thanks , got the basic Idea... – Archana Sarang Sep 06 '14 at 13:38