我不太了解android,因为我正在开发一个Android应用程序,我想首先访问我的数据库来完成一个登录功能。我使用eclipse,wamp服务器和一点点php。
这是我的java类:
package com.example.marketapp;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class Login extends ActionBarActivity implements OnClickListener{
EditText EmailAdresst, Passwordt;
Button blogin,btnregister;
String email, password;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
private static InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initialise();
}
private void initialise(){
EmailAdresst = (EditText) findViewById(R.id.EmailAdresst);
Passwordt = (EditText) findViewById(R.id.Passwordt);
blogin = (Button) findViewById(R.id.btnlogin);
// btnregister = findViewById(R.id.btnregister);
blogin.setOnClickListener(this);
//btnregister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://127.0.0.1/test/index.php");
email = EmailAdresst.getText().toString();
password = Passwordt.getText().toString();
try{
//Create new Array List
nameValuePairs = new ArrayList<NameValuePair>();
//Place them in an array List
nameValuePairs.add(new BasicNameValuePair("email",email));
nameValuePairs.add(new BasicNameValuePair("password",password));
//Add array list to http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//assign executed from container to response
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 retmail = jsonResponse.getString("mail");
String retpass = jsonResponse.getString("pass");
//validate login
if(email.equals(retmail)&& password.equals(retpass)){
//Create a new shared preference by getting the preference
//Give the shared preference any name you like
SharedPreferences sp = getSharedPreferences("Logindetails",0);
//Edit the Shared Preference
SharedPreferences.Editor spedit = sp.edit();
//Put the login details as strings
spedit.putString("mail", email);
spedit.putString("pass", password);
//Close the editor
spedit.commit();
//Display a Toast saying login was a success
Toast.makeText(getBaseContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();
}else{
//display a toast saying it failed.
Toast.makeText(getBaseContext(), "Invalid Login Details", Toast.LENGTH_SHORT).show();
}
}
}
}catch(Exception e){
e.printStackTrace();
//Display toast when there is a connection error
Toast.makeText(getBaseContext(), "Connection Error", Toast.LENGTH_SHORT).show();
}
}
private static String convertStreamToString(InputStream instream) {
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();
}
}}
Here is my activity_login.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue"
android:orientation="vertical"
android:padding="30dp" >
<TextView
android:id="@+id/WelcomeL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:text="Καλώς ήρθατε!"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/welcomet"
android:layout_width="279dp"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Eδώ θα βρείτε τις προσφορές των προιόντων μας καθώς και τις προσωποποιημένες μας προσφορές για κάθε πελάτη. "
android:textColor="#ffff" />
<EditText
android:id="@+id/EmailAdresst"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/round_rect_shape"
android:ems="10"
android:hint="name@email.com"
android:inputType="textEmailAddress"
android:padding="8dp"
android:textSize="10sp" />
<EditText
android:id="@+id/Passwordt"
android:layout_width="239dp"
android:layout_height="wrap_content"
android:background="@drawable/round_rect_shape"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
android:padding="8dp"
android:textSize="10sp" />
<LinearLayout
android:layout_width="263dp"
android:layout_height="84dp"
android:layout_marginRight="10dp" >
<Button
android:id="@+id/btnregister"
android:layout_width="104dp"
android:layout_height="52dp"
android:text="Register"/>
<Button
android:id="@+id/btnlogin"
android:layout_width="111dp"
android:layout_height="52dp"
android:text="Login"/>
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="241dp"
android:layout_height="60dp"
android:layout_weight="0.38"
android:text="Ξεχάσατε τον κωδικό πρόσβασής σας ;"
android:textColor="#ffffff"
android:textSize="12sp" />
</LinearLayout>
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.marketapp"
android:vesionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/offer"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".UserPage">
</activity>
<activity
android:name=".Login">
</activity>
<!-- account Activity -->
<activity android:name=".AccountActivity" />
<!-- General Offers Activity -->
<activity android:name=".GenOffers" />
<!-- Personal Offers Activity -->
<activity android:name=".PersonOffers" />
<activity
android:name=".changePassword">
</activity>
</application>
</manifest>
And here is my index.php file:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
error_reporting(E_ERROR | E_PARSE);
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbdb="market";
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
mysql_select_db($dbdb) or die ("database selection error");
$email =$_POST['email'];
$password =$_POST['password'];
$query= mysql_query("SELECT * FROM users WHERE email='$email' AND password='$password'");
$num = mysql_num_rows($query);
if ($num==1){
while($list=mysql_fetch_assoc($query))
{
$output =$list;
echo json_encode($output);
}
mysql_close();
}
?>
我在phpmyadmin中创建了一个名为“market”的数据库和一个包含2个字段“email”和“password”以及3个记录的表用户。我在eclipse的模拟器上运行应用程序没有错误,当我放置一个记录并单击登录按钮时总是得到“连接错误”。据我所知,这是一个数据库问题。有什么建议吗?