服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android+json+php+mysql实现用户反馈功能方法解析

android+json+php+mysql实现用户反馈功能方法解析

2020-12-18 14:58Android教程网 Android

相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流

相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流。首先看具体界面,三个字段。名字,邮箱为选填,可以为空,建议不能为空。如有需要可以给我留言。
android+json+php+mysql实现用户反馈功能方法解析 
下面贴出布局代码,这里用到一个<include layout="@layout/uphead">就是把另外一个布局文件引入到这个布局中。

复制代码 代码如下:


<?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:orientation="vertical"
android:background="@color/bg_gray" >
<include layout="@layout/uphead"/>
<!-- name label -->
<textview android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="名字(选填)"
android:paddingleft="10dip"
android:paddingright="10dip"
android:textcolor="@color/coffee"
android:paddingtop="10dip"
android:textsize="12sp"/>
<!-- input name -->
<edittext android:id="@+id/inputname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginbottom="15dip"
android:singleline="true"/>
<!-- price label -->
<textview android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮箱(选填)"
android:paddingleft="10dip"
android:paddingright="10dip"
android:textcolor="@color/coffee"
android:paddingtop="10dip"
android:textsize="12sp"/>
<!-- input price -->
<edittext android:id="@+id/inputemail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginbottom="15dip"
android:singleline="true"/>
<!-- description label -->
<textview android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="建议(必填)"
android:paddingleft="10dip"
android:paddingright="10dip"
android:paddingtop="10dip"
android:textcolor="@color/coffee"
android:textsize="12sp"/>
<!-- input description -->
<edittext android:id="@+id/inputdesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginbottom="15dip"
android:lines="4"
android:gravity="top"/>
<!-- button create product -->
<button android:id="@+id/btncreateproduct"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textsize="20sp"
android:textcolor="@color/coffee"
/>
</linearlayout>


下面贴出uphead的布局代码,里面用到一个textview,一个button为返回按钮。

复制代码 代码如下:


<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/top" >
<textview
android:id="@+id/tv_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerinparent="true"
android:shadowcolor="#ff000000"
android:shadowdx="2"
android:shadowdy="0"
android:shadowradius="1"
android:text=""
android:textcolor="@color/white"
android:textsize="18sp"
android:textstyle="bold" />
<button
android:id="@+id/upback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignparentleft="true"
android:layout_centervertical="true"
android:layout_marginleft="17dp"
android:drawableleft="@id/tv_head"
android:background="@drawable/back" />
</relativelayout>


下面贴出android客户端代码,三个类,一个用于与服务器交互发送post请求,以及json的传递。还有一个dailog实例。

复制代码 代码如下:


package com.android.up;
import java.util.arraylist;
import java.util.list;
import org.apache.http.httpresponse;
import org.apache.http.namevaluepair;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.message.basicnamevaluepair;
import org.json.jsonobject;
import com.android.mainactivity;
import com.android.r;
import com.anroid.net.dialogutil;
import android.app.activity;
import android.app.progressdialog;
import android.content.intent;
import android.os.asynctask;
import android.os.bundle;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
public class up extends activity {
// progress dialog
private progressdialog pdialog;
private textview tv_head;
jsonparser jsonparser = new jsonparser();
edittext inputname;
edittext inputemail;
edittext inputdesc;
button upback;
// url to create new product
private static string url_up = "http://10.0.2.2/up/up.php";//此处写的是你的服务器端的地址
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.up);
tv_head = (textview)findviewbyid(r.id.tv_head);
tv_head.settext("建议");
// edit text
inputname = (edittext) findviewbyid(r.id.inputname);
inputemail = (edittext) findviewbyid(r.id.inputemail);
inputdesc = (edittext) findviewbyid(r.id.inputdesc);
upback = (button)findviewbyid(r.id.upback);
upback.setonclicklistener(new onclicklistener() {
@override
public void onclick(view arg0) {
// todo auto-generated method stub
intent back = new intent(up.this,mainactivity.class);
back.setflags(intent.flag_activity_clear_top);
startactivity(back);
up.this.finish();
}
});
// create button
button btncreateproduct = (button) findviewbyid(r.id.btncreateproduct);
// button click event
btncreateproduct.setonclicklistener(new view.onclicklistener() {
public void onclick(view view) {
// creating new product in background thread
if(validate()){
new up().execute();
}
}
});
}
private boolean validate()
{
string description = inputdesc.gettext().tostring().trim();
if (description.equals(""))
{
dialogutil.showdialog(this, "您还没有填写建议", false);
return false;
}
return true;
}
/**
* background async task to create new product
* */
class up extends asynctask<string, string, string> {
/**
* before starting background thread show progress dialog
* */
@override
protected void onpreexecute() {
super.onpreexecute();
pdialog = new progressdialog(up.this);
pdialog.setmessage("正在上传..");
pdialog.setindeterminate(false);
pdialog.setcancelable(true);
pdialog.show();
}
/**
* creating product
* */
protected string doinbackground(string... args) {
string name = inputname.gettext().tostring();
string price = inputemail.gettext().tostring();
string description = inputdesc.gettext().tostring();
// building parameters
list<namevaluepair> params = new arraylist<namevaluepair>();
params.add(new basicnamevaluepair("name", name));
params.add(new basicnamevaluepair("email", price));
params.add(new basicnamevaluepair("description", description));
// getting json object
// note that create product url accepts post method
try{
jsonobject json = jsonparser.makehttprequest(url_up,
"post", params);
}catch(exception e){
e.printstacktrace();
}
// check for success tag
return null;
}
/**
* after completing background task dismiss the progress dialog
* **/
protected void onpostexecute(string file_url) {
pdialog.setmessage("上传成功");
pdialog.dismiss();

}
}
}


下面贴出dailog实例类

复制代码 代码如下:


/**
*
*/
package com.anroid.net;
import android.app.alertdialog;
import android.content.context;
import android.content.dialoginterface;
import android.content.dialoginterface.onclicklistener;
import android.view.view;
import android.app.activity;
public class dialogutil
{
// 定义一个显示消息的对话框
public static void showdialog(final context ctx
, string msg , boolean closeself)
{
// 创建一个alertdialog.builder对象
alertdialog.builder builder = new alertdialog.builder(ctx)
.setmessage(msg).setcancelable(false);
if(closeself)
{
builder.setpositivebutton("确定", new onclicklistener()
{
public void onclick(dialoginterface dialog, int which)
{
// 结束当前activity
((activity)ctx).finish();
}
});
}
else
{
builder.setpositivebutton("确定", null);
}
builder.create().show();
}
// 定义一个显示指定组件的对话框
public static void showdialog(context ctx , view view)
{
alertdialog.builder builder = new alertdialog.builder(ctx)
.setview(view).setcancelable(false)
.setpositivebutton("确定", null);
builder.create()
.show();
}
}


剩下就是如何与服务器端交互了不多说,代码如下

复制代码 代码如下:


package com.android.up;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.unsupportedencodingexception;
import java.util.list;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.namevaluepair;
import org.apache.http.client.clientprotocolexception;
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.protocol.http;
import org.json.jsonexception;
import org.json.jsonobject;
import android.util.log;
public class jsonparser {
static inputstream is = null;
static jsonobject jobj = null;
static string json = "";
// constructor
public jsonparser() {
}
// function get json from url
// by making http post
public jsonobject makehttprequest(string url, string method,
list<namevaluepair> params) {
// making http request
try {
// request method is post
// defaulthttpclient
defaulthttpclient httpclient = new defaulthttpclient();
httppost httppost = new httppost(url);
httppost.setentity(new urlencodedformentity(params,http.utf_8));
httpresponse httpresponse = httpclient.execute(httppost);
httpentity httpentity = httpresponse.getentity();
is = httpentity.getcontent();
} catch (unsupportedencodingexception e) {
e.printstacktrace();
} catch (clientprotocolexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
try {
bufferedreader reader = new bufferedreader(new inputstreamreader(
is, "utf-8"));
stringbuilder sb = new stringbuilder();
string line = null;
while ((line = reader.readline()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.tostring();
} catch (exception e) {
log.e("buffer error", "error converting result " + e.tostring());
log.d("json", json.tostring());
}
// try parse the string to a json object
try {
jobj = new jsonobject(json);
} catch (jsonexception e) {
log.e("json parser", "error parsing data " + e.tostring());
}
// return json string
return jobj;
}
}


到此android客户端已经完成,后天服务器端用php+mysql实现,当然这里只是个实例,存取到数据库里面,没有进行展示,代码如下

复制代码 代码如下:


<?php
// array for json response
$response = array();
include("conn.php");
// check for required fields
if (isset($_post['name']) && isset($_post['email']) && isset($_post['description'])) {
$name = $_post['name'];
$price = $_post['email'];
$description = $_post['description'];
$result = mysql_query("insert into up(name, email, description) values('$name', '$email', '$description')");
echo $result;
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "product successfully created.";
// echoing json response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "oops! an error occurred.";
// echoing json response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "required field(s) is missing";
// echoing json response
echo json_encode($response);
}
?>


数据库表结构如下,连接数据库代码就不贴出了,记得把编码设置为utf-8就行了。

android+json+php+mysql实现用户反馈功能方法解析
到此就完成了一个用户反馈的基本功能,后台数据里展示。如有问题欢迎给我留言。

延伸 · 阅读

精彩推荐