我正在开发一个需要用户登录的应用。它不是在Jelly Bean中工作,而是在GingerBread中工作。任何想法?
这是代码。
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
这是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" />
这是login.php文件:
<?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();
?>
答案 0 :(得分:0)
这里您的问题的解决方案使用AsyncTask进行Web请求我已经使用HttpClient然后HttpPost在Web上发出请求。
//
新的AsyncTask(){
@Override
protected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://182.188.140.167/gpsweb/check%20username.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(httppost);
HttpEntity resEntityGet = response.getEntity();
if (resEntityGet != null)
{
//Log.e("Checking responce", EntityUtils.toString(resEntityGet).toString());
String response1 = EntityUtils.toString(resEntityGet).toString();
if(response1.contains("1"))
{
Intent i=new Intent(MainActivity.this,GetLocation.class);
Bundle b=new Bundle();
b.putString("uname",uname);
i.putExtras(b);
startActivity(i);
}
else
{
return 0;
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return 2;
}
答案 1 :(得分:0)
你可能需要添加适当的权限去你的Manifest。
<uses-permission android:name="android.permission.INTERNET"/>