当前位置: 编程技术>移动开发
android+json+php+mysql实现用户反馈功能方法解析
来源: 互联网 发布时间:2014-10-11
本文导语: 相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流。首先看具体界面,三个字段。名字,邮箱为选填,可以为空,建议不能为空。如有需要可以给我留言。 下面贴出布局代码...
相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流。首先看具体界面,三个字段。名字,邮箱为选填,可以为空,建议不能为空。如有需要可以给我留言。
下面贴出布局代码,这里用到一个就是把另外一个布局文件引入到这个布局中。
下面贴出uphead的布局代码,里面用到一个TextView,一个Button为返回按钮。
下面贴出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 {
/**
* 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 params = new ArrayList();
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 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实现,当然这里只是个实例,存取到数据库里面,没有进行展示,代码如下
解析ADT-20问题 android support library
android layout XML解析错误的解决方法
Android入门之TableLayout应用解析(一)
解析:android 如何从JPEG生成BufferedImage
基于Android XML解析与保存的实现
基于android中读取assets目录下a.txt文件并进行解析的深入分析
解析Android应用启动后自动创建桌面快捷方式的实现方法
Android入门之TableLayout应用解析(二)
解析Android中string-array数据源的简单使用
解析android创建快捷方式会启动两个应用的问题
解析离线安装Eclipse的Android ADT开发插件的具体操作(图文)
解析后台进程对Android性能影响的详解
解析Android横竖屏切换的问题
解析android中的帮助、about、关于作者、HELP等提示页面
Android中asset文件夹与raw文件夹的区别深入解析
解析Android应用程序运行机制
android4.0与2.3版本的TP代码区别解析
解析Android中webview和js之间的交互
Android中Parcelable的作用实例解析
下面贴出布局代码,这里用到一个就是把另外一个布局文件引入到这个布局中。
代码如下:
下面贴出uphead的布局代码,里面用到一个TextView,一个Button为返回按钮。
代码如下:
下面贴出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 {
/**
* 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 params = new ArrayList();
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 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实现,当然这里只是个实例,存取到数据库里面,没有进行展示,代码如下
代码如下: