示例
以下示例是通过PHP页面将android应用程序与MYSQL数据库连接的完整示例。它创建一个基本的应用程序,允许您使用GET和POST方法登录。
PHP-MYSQL部分
在此示例中,已在000webhost.com中创建了名称为temp的数据库。在该数据库中,已创建一个名为table1的表。该表具有三个字段。(Username, Password, Role)。该表只有一条记录(“admin”,“admin”,“administrator”)。下面给出了php页面,该页面通过post方法获取参数。
<?php
$con=mysqli_connect("example.com","username","password","db_name");
if (mysqli_connect_errno($con)) {
echo " MySQL链接失败: " . mysqli_connect_error();
}
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$result = mysqli_query($con,"SELECT Role FROM table1 where
Username='$username' and Password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
mysqli_close($con);
?>
Android 部分
要尝试使用此示例,您需要在连接了wifi互联网的实际设备上运行此示例。
- 您将使用Android Studio创建一个Android应用程序,并将其命名为Demo,位于com.jc2182.demo包下,如Hello World示例一章中所述。
- 修改src/MainActivity.java文件以添加活动代码。
- 创建src/DoRequest.java文件以添加HTTP请求代码。
- 修改布局XML文件res/layout/activity_main.xml如果需要,可添加任何GUI组件。
- 修改res/values/string.xml文件并添加必要的字符串组件。
- 修改AndroidManifest.xml以添加必要的权限。
- 运行该应用程序以启动Android模拟器并验证在该应用程序中所做更改的结果。
以下是修改后的主要活动文件src/com.jc2182.demo/MainActivity.java的内容。
package com.jc2182.demo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends Activity {
private EditText usernameField,passwordField;
private TextView status,role,method;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameField = (EditText)findViewById(R.id.editText1);
passwordField = (EditText)findViewById(R.id.editText2);
status = (TextView)findViewById(R.id.textView6);
role = (TextView)findViewById(R.id.textView7);
method = (TextView)findViewById(R.id.textView9);
}
public void showMsg(String msg){
role.setText(msg);
status.setText("登录成功");
}
public void login(View view) throws IOException {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
Message message = Message.obtain();
try {
DoRequest dr = new DoRequest();
dr.doInBackground(username,password,1);
String res = dr.getReturnMsg();
showMsg(res);
method.setText("Get 方法");
} catch (IOException e) {
e.printStackTrace();
}
Looper.loop();
}
}).start();
}
public void loginPost(View view) throws IOException {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
Message message = Message.obtain();
try {
DoRequest dr = new DoRequest();
dr.doInBackground(username,password,1);
String res = dr.getReturnMsg();
showMsg(res);
method.setText("Post 方法");
} catch (IOException e) {
e.printStackTrace();
}
Looper.loop();//增加部分
}
}).start();
}
}
以下是修改后的主要活动文件src/com.jc2182.demo/DoRequest.java的内容。
package com.jc2182.demo;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class DoRequest {
/**
* 返回消息
*/
protected String returnMsg;
public String getReturnMsg(){
return this.returnMsg;
}
public void doInBackground(String username,String password,int byGetOrPost) throws IOException {
if(byGetOrPost == 0) {
//get的方式提交就是url拼接的方式
String path = "http://192.168.61.201:9090/login.php?username=" + username + "&password=" + password;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(path)
.get() // 默认get 不显式调用也可以
.build();
Call call = client.newCall(request);
Response response = call.execute();
//第四步,解析响应结果
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
Log.d("GET返回头", headers.name(i) + ":" + headers.value(i));
}
this.returnMsg = response.body().string();
}else {
String path = "http://192.168.61.201:9090/login.php";
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("username", username)
.add("password",password)
.build();
Request request = new Request.Builder()
.url(path)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
Log.d("POST返回头", headers.name(i) + ":" + headers.value(i));
}
this.returnMsg = response.body().string();
}
}
}
以下是res/layout/activity_main.xml文件的内容-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView3"
android:layout_width="79dp"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:layout_marginTop="117dp"
android:text="@string/App"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="68dp"
android:layout_height="27dp"
android:layout_below="@+id/textView3"
android:layout_marginTop="73dp"
android:text="@string/Username" />
<TextView
android:id="@+id/textView2"
android:layout_width="64dp"
android:layout_height="35dp"
android:layout_below="@+id/textView1"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:layout_marginTop="28dp"
android:layout_marginBottom="-56dp"
android:text="@string/Password" />
<EditText
android:id="@+id/editText1"
android:layout_width="326dp"
android:layout_height="30dp"
android:layout_below="@+id/textView3"
android:layout_alignParentEnd="true"
android:layout_marginTop="31dp"
android:layout_marginEnd="3dp">
<requestFocus android:layout_width="wrap_content" />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="278dp"
android:layout_height="29dp"
android:layout_below="@+id/editText1"
android:layout_alignEnd="@+id/editText1"
android:layout_marginStart="5dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="4dp"
android:layout_toEndOf="@+id/textView2"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="121dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignStart="@+id/textView2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentStart="true"
android:layout_marginStart="-3dp"
android:layout_marginBottom="45dp"
android:onClick="login"
android:text="@string/LoginGet" />
<Button
android:id="@+id/button2"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="1dp"
android:layout_toEndOf="@+id/textView6"
android:onClick="loginPost"
android:text="@string/LoginPost" />
<TextView
android:id="@+id/textView8"
android:layout_width="63dp"
android:layout_height="28dp"
android:layout_below="@+id/button1"
android:layout_alignStart="@+id/textView5"
android:layout_marginStart="-2dp"
android:layout_marginBottom="-20dp"
android:text="@string/method" />
<TextView
android:id="@+id/textView4"
android:layout_width="66dp"
android:layout_height="28dp"
android:layout_below="@+id/textView8"
android:layout_marginStart="23dp"
android:layout_marginTop="13dp"
android:text="@string/LoginStatus" />
<TextView
android:id="@+id/textView5"
android:layout_width="70dp"
android:layout_height="30dp"
android:layout_below="@+id/textView4"
android:layout_marginTop="43dp"
android:layout_marginEnd="7dp"
android:layout_toStartOf="@+id/editText1"
android:text="@string/LoginRole" />
<TextView
android:id="@+id/textView7"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_alignBottom="@+id/textView5"
android:layout_marginBottom="0dp"
android:layout_toRightOf="@+id/textView5"
android:text="@string/Role"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10sp"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/textView6"
android:layout_width="100dp"
android:layout_height="32dp"
android:layout_alignTop="@+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:layout_toEndOf="@+id/textView4"
android:text="@string/Status"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10sp"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/textView9"
android:layout_width="96dp"
android:layout_height="29dp"
android:layout_below="@+id/button2"
android:layout_alignStart="@+id/textView6"
android:layout_alignBottom="@+id/textView8"
android:layout_marginStart="56dp"
android:layout_marginTop="11dp"
android:layout_marginBottom="-1dp"
android:text="@string/Choose"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10sp"
tools:ignore="SmallSp" />
</RelativeLayout>
以下是AndroidManifest.xml文件的内容-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jc2182.demo">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:usesCleartextTraffic="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondMain"></activity>
</application>
</manifest>
让我们尝试运行刚刚修改的应用程序。我假设您在进行环境设置时已创建了AVD。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后工具栏中单击“运行”图标。Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示在“模拟器”窗口下面-
尝试POST , GET 请求 分别如下: