I am developing an app which require login of users. It is not working in Jelly Bean but working in GingerBread. Any Ideas?
Here is code.
MainActivity.java:
package com.example.test;
public class MainActivity extends Activity {
String sname,spass;
EditText sname1,spass1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sname1 = (EditText) findViewById(R.id.editText1);
spass1 = (EditText) findViewById(R.id.editText2);
Button login=(Button) findViewById(R.id.button1);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sname = sname1.getText().toString();
spass = spass1.getText().toString();
BufferedReader reader=null;
try
{
String data= "&" + URLEncoder.encode("sname", "UTF-8") + "=" + URLEncoder.encode(sname, "UTF-8");
data += "&" + URLEncoder.encode("spass", "UTF-8") + "=" + URLEncoder.encode(spass, "UTF-8");
String text = null;
String temp1=null;
// Send data
URL url = new URL("http://10.0.2.2/login.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
text = sb.toString();
temp1=text.trim();
if(temp1.contains("notequal"))
{
Toast.makeText(getApplicationContext(),"Wrong Code or Id", Toast.LENGTH_SHORT).show();
}
else if(temp1.contains("equalto"))
{
Toast.makeText(getApplicationContext()," Login Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Second.class));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {ex.printStackTrace();}
}
}
});//login
}//oncreate
}//main
Here is activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Name" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="21dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginTop="16dp"
android:text="Password" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_toRightOf="@+id/textView3"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/editText2"
android:layout_marginTop="17dp"
android:text="Login" />
Here is login.php file:
<?php
$sname = urldecode($_POST['sname']);
$spass = urldecode($_POST['spass']);
mysql_connect("localhost","root","");
mysql_select_db("paper");
$sql=mysql_query("select * from student where name='$sname' and password='$spass'");
if($row=mysql_fetch_assoc($sql)){
echo "equalto";
}
else{
echo "notequal";}
mysql_close();
?>