1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
   |  
package com.example.myschoolapplicationproject;
 
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
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;
 
//import com.example.androidhive.library.DatabaseHandler;
//import com.example.myschoolapplicationproject.library.UserFunctions;
import com.example.myschoolapplicationproject.*; 
@SuppressLint("NewApi")
public class LoginActivity extends Activity implements OnClickListener{
 
 EditText loginEmail, loginPassword;
 Button btnLogin;
 
 String username,password;
 HttpClient httpclient;
 HttpPost httppost;
 ArrayList<NameValuePair> nameValuePairs;
 HttpResponse response;
 HttpEntity entity;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
 
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
 
       initialise();
 
    }
       private void initialise(){
       loginEmail=(EditText)findViewById(R.id.loginEmail);
       loginPassword=(EditText)findViewById(R.id.loginPassword);
       btnLogin =(Button) findViewById(R.id.btnLogin);
       btnLogin.setOnClickListener(this);
       }
        // Link to Register Screen
       /* btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        RegisterActivity.class);
                startActivity(i);
                finish();
            }
        });*/
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		httpclient= new DefaultHttpClient();
		httppost= new HttpPost ("http://10.0.2.2/AndroidConnect/get_user.php");
	    username=loginEmail.getText().toString();
	    password=loginPassword.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){
	    		entity= response.getEntity();
	    		if(entity!=null){
	    			InputStream instream= entity.getContent();
	    			//la ligne suivante est la source de l'erreur
	    			JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
		   		     Log.e("connnnnn errooooor","connnnn erroooooor");
	    			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("password",password);
	    				spedit.commit();
	    				Toast.makeText(getBaseContext(), "SUCCES§", Toast.LENGTH_SHORT);
	    				Log.d("success","welcome");
	    			}else{
	    				Log.d("error","invalid log details");
	    				Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
	    			}
	    		}
	    	}
	    }catch(Exception e){
	     e.printStackTrace();	
	     Toast.makeText(getBaseContext(), "Connection Error",Toast.LENGTH_SHORT).show();
	    }
	}
		private Map convertStreamToString(InputStream instream) {
			// TODO Auto-generated method stub
			return null;
		}
    } | 
Partager