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:
package com.example.logintutorial;
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.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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{
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...???
0 comments:
Post a Comment