package com.example.login;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText etUser, etPass;
Button bLogin;
String username, password;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialise();
}
private void initialise(){
etUser =(EditText) findViewById(R.id.etUser);
etPass = (EditText) findViewById(R.id.etPass);
bLogin = (Button)findViewById(R.id.bSubmit);
bLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
httpclient= new DefaultHttpClient();
httppost = new HttpPost("http://10.0.2.2/login/index.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){
entity=response.getEntity();
if(entity != null){
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
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(getBaseContext(),"SUCCESS", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(),"Invalid Logind Details", Toast.LENGTH_SHORT).show();
}
}
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(),"Login Unsuccessfull", Toast.LENGTH_SHORT).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 e){
e.printStackTrace();
}
}
return sb.toString();
}
}
Asked
Active
Viewed 49 times
0
Rushabh Patel
- 3,052
- 4
- 26
- 58
-
1Must Check that your Username and password posted properly and successfuly and also must sure that you are using same key which is in your Json Response. Post your logcat also. – Piyush Mar 05 '14 at 09:13
-
1post your logcat, check for exceptions , esp when the click happens – Pararth Mar 05 '14 at 09:14
-
first use AsyncTask or Thread to communicate with server.If not then you get NetworkOnMainThreadException exception above api7. – mukesh Mar 05 '14 at 09:23
-
the problem is the the htpclient is not executing the httppost i don't know why, i am sure he user and pass posted are the same. – user3382695 Mar 05 '14 at 10:18
-
@user3382695 you are trying to perform a networking operation on its main thread. – Pararth Mar 05 '14 at 10:37
1 Answers
0
You must be getting the NetworkOnMainThreadException , check your logcat. Also pls post it. Will edit/remove my answer if its not that.
It'l be good to know why it occurs and how can you use async task:
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
http://developer.android.com/training/articles/perf-anr.html
How to fix android.os.NetworkOnMainThreadException?
Android - android.os.NetworkOnMainThreadException