服務器採用本地的apache服務器,全部的php文件都寫在D:\AppServ\www目錄下。。以前也一直在寫請求的代碼,一直沒有成功,緣由是請求的url有問題。。請求的url必定要跟本身服務器的端口號一致。如何改變端口號,參看 :http://jingyan.baidu.com/article/a65957f4fe8ec424e67f9bff.html php
設置好本身的端口號以後,還須要注意一下請求的url格式: html
該php文件在本地保存的地址是這樣的 :D:\AppServ\www\get_data.json java
在android客戶端 中:url-------------- "http://10.0.2.2:8080/get_data.json"
在網頁 :HTTP http://127.0.0.1:8080/get_data.json/ mysql
這裏必定要弄正確。。而後啓動apache android
一,客戶端準備 web
須要加入網絡權限,因爲網絡請求時耗時操做,因此把請求的內容都寫在了一個繼承與thread的子類中。 sql
1.MainActivity.java 數據庫
package com.example.log_user; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.os.Build; public class MainActivity extends ActionBarActivity { EditText user_name; EditText pass_word; Button login; Button zhuce; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); user_name=(EditText) findViewById(R.id.editText1); pass_word=(EditText) findViewById(R.id.editText2); login=(Button) findViewById(R.id.button1); zhuce=(Button) findViewById(R.id.button2); login.setOnClickListener(new OnClickListener(){ String url="http://10.0.2.2:8080/config.inc.php"; @Override public void onClick(View arg0) { String user=user_name.getText().toString(); String pass=pass_word.getText().toString(); // TODO Auto-generated method stub new LoginThread(user,pass,url).start(); Log.d("MAIN","-------------------->MAINSUCCESS"); } }); zhuce.setOnClickListener(new OnClickListener(){ String url="http://10.0.2.2:8080/test_signup.php"; @Override public void onClick(View arg0) { // TODO Auto-generated method stub String user=user_name.getText().toString(); String pass=pass_word.getText().toString(); new signup(user,pass,url).start(); Log.d("Main","------------------->signupsuccess"); } }); } }
2.LoginThread.java登陸 apache
簡單說一下這個類,以前也由於沒有意識到須要新建子線程而犯了不少錯誤。繼承於thread,把須要執行的操做放在run方法裏。gotoLogin方法用來判斷輸入用戶名和密碼是否與服務器所連數據庫裏面的字段匹配。這裏用了httpClient來處理post請求,須要將傳遞的數據放在這個數組裏。 json
ArrayList<NameValuePair> params在執行了post請求以後
HttpResponse respose=client.execute(post);會返回一個httpresponse,將這個對象轉爲string以後其實就是請求的php url頁面所echo 出來的字符串。
String content=EntityUtils.toString(respose.getEntity());
package com.example.log_user; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; 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.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.content.res.Resources.Theme; import android.util.Log; import android.widget.Toast; public class LoginThread extends Thread { String username; String password; String Url; @Override public void run() { // TODO Auto-generated method stub super.run(); Log.d("run","-------------------->MAINSUCCESS"); boolean isLoginSuccess=gotoLogin(username,password,Url); if(isLoginSuccess){ Log.d("Log","--------------------->登陸成功"); } else { Log.d("Log","--------------------->登陸失敗"); } } public LoginThread(String user,String pass,String url){ this.username=user; this.password=pass; this.Url=url; } public boolean gotoLogin(String user,String pass,String url){ boolean issuccess=false; String result; //發送post請求 HttpClient client=new DefaultHttpClient(); HttpPost post = new HttpPost(url); //Post運做傳送變數必須用NameValuePair[]陣列儲存 ArrayList<NameValuePair> params=new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name",user)); params.add(new BasicNameValuePair("password",pass)); try { post.setEntity(new UrlEncodedFormEntity(params)); try { HttpResponse respose=client.execute(post); if(respose.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String content=EntityUtils.toString(respose.getEntity()); Log.d("post","--------------------->success"); Log.d("post",content+""); if(content.equals("logsuccess")){ issuccess=true; } } else { Log.d("post","--------------------->failes"); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return issuccess; } }3.signup.java註冊頁面 :原理與登陸相似
package com.example.log_user; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; 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.apache.http.util.EntityUtils; import android.util.Log; public class signup extends Thread { private String username; private String password; private String url; @Override public void run() { // TODO Auto-generated method stub super.run(); boolean issuccess=doPOSThttp(username,password,url); if(issuccess){ Log.d("sign","-------------->signsuccess"); } else { Log.d("sign","-------------->signfailue"); } } public signup(String user,String password,String url){ this.username=user; this.password=password; this.url=url; } public boolean doPOSThttp(String username,String password,String url){ boolean issuccess=false; HttpClient client=new DefaultHttpClient(); HttpPost post =new HttpPost(url); ArrayList<NameValuePair> param=new ArrayList<NameValuePair>(); param.add(new BasicNameValuePair("name",username)); param.add(new BasicNameValuePair("password",password)); try { post.setEntity(new UrlEncodedFormEntity(param)); try { HttpResponse response=client.execute(post); if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String cont=EntityUtils.toString(response.getEntity()); Log.d("post","------------>"+cont); if(cont.equals("signsuccess")) { issuccess=false; } } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return issuccess; } }
二。服務器準備
1,config.inc.php 這個是與客戶端的登陸請求對應。經過$_POST方法取到傳遞的數據,在於數據庫裏面的數據匹配一下,匹配成功,則echo 一個標示字符串,用來使客戶端用response取到該字符串進行判斷。
<? $db_host=localhost; $db_user="root"; $db_pass="123456789"; $db_name="stu"; $table_name="student"; /* mysql_query('set names utf8'); */ if($con=mysql_connect($db_host,$db_user,$db_pass)) { } else { echo "鏈接失敗"; } if(mysql_select_db(web_01)) { } else{ echo "選擇數據庫失敗"; } $username=$_POST['name']; $sql="select * from my_user where name='$username'"; $query=mysql_query($sql); if($row=mysql_fetch_array($query)){ if($_POST['password']==$row['password']) echo "logsuccess"; } mysql_close($con); ?>
2.test_signup.php 這個與客戶端的註冊請求對應。
<? $db_host=localhost; $db_user="root"; $db_pass="123456789"; $db_name="stu"; $table_name="student"; /* mysql_query('set names utf8'); */ if($con=mysql_connect($db_host,$db_user,$db_pass)) { } else { echo "鏈接失敗"; } if(mysql_select_db(web_01)) { } else{ echo "選擇數據庫失敗"; } $username=$_POST['name']; $pass=$_POST['password']; if(mysql_query('insert into my_user(name,password) values($username,$pass)')){ echo "signsuccess"; } mysql_close($con); ?>