使用SAE的服务来实现android端的用户反馈功能。
这篇是上个月在SAE论坛上写的,现在也转过来吧。
因为应用的需要在SAE开了个jvm来帮android端合并一些请求,提供一些查询和抓取服务。但是SAE的jvm比较贵,每个小时都要5云豆,所以就打算搞点其他的功能,搭建一个简单的服务端,根据需要添加其他功能。
首先想到写个用户反馈的功能,这里需要先在SAE应用里建立一个MySQL数据库,具体创建可以参考SAE的文档。
先是Android端提交数据的代码:
1 public class UserMessage extends Activity implements OnClickListener { 2 private static final String TAG = UserMessage.class.getSimpleName(); 3 private EditText tvText, tvMail; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_gettext); 8 tvText = (EditText) findViewById(R.id.user_text); 9 tvMail = (EditText) findViewById(R.id.user_mail); 10 Button button = (Button) findViewById(R.id.user_bt); 11 button.setOnClickListener(this); 12 TextView userInfo = (TextView) findViewById(R.id.user_info); 13 userInfo.setText(getInfo()); 14 } 15 // 获得机型、android版本、应用版本号 16 private String getInfo() { 17 PackageManager pManager = getPackageManager(); 18 PackageInfo pInfo = null; 19 try { 20 pInfo = pManager.getPackageInfo(getPackageName(), 0); 21 } catch (NameNotFoundException e) { 22 Log.e(TAG, "包名不存在"); 23 e.printStackTrace(); 24 } 25 StringBuilder info = new StringBuilder(); 26 info.append("机型:" + android.os.Build.MODEL); 27 info.append(" Android" + android.os.Build.VERSION.RELEASE); 28 info.append(" 软件版本:" + pInfo.versionName); 29 return info.toString(); 30 } 31 @Override 32 public void onClick(View v) { 33 String text = tvText.getText().toString().trim(); 34 String mail = tvMail.getText().toString().trim(); 35 if (TextUtils.isEmpty(text)) { 36 Toast.makeText(UserMessage.this, "内容不能为空", Toast.LENGTH_SHORT).show(); 37 } else { 38 new SendMessageTask().execute(new String[] { mail, text }); 39 } 40 } 41 class SendMessageTask extends AsyncTask<String, Integer, String> { 42 protected String doInBackground(String... params) { 43 // 用POST方式提交 44 HttpPost httpRequest = new HttpPost("http://****.sinaapp.com/ReceiveMessage"); 45 ArrayList<NameValuePair> list = new ArrayList<NameValuePair>(); 46 list.add(new BasicNameValuePair("mail", params[0])); 47 list.add(new BasicNameValuePair("text", params[1])); 48 list.add(new BasicNameValuePair("info", getInfo())); 49 String result = ""; 50 try { 51 httpRequest.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); 52 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 53 // 获取返回的状态码 54 if (httpResponse.getStatusLine().getStatusCode() == 200) { 55 // 获取返回的信息 56 result = EntityUtils.toString(httpResponse.getEntity()); 57 } else { 58 result = "发送失败"; 59 } 60 } catch (Exception e) { 61 e.printStackTrace(); 62 return "发送失败"; 63 } 64 return result; 65 } 66 protected void onPreExecute() { 67 Toast.makeText(UserMessage.this, "正在发送中", Toast.LENGTH_SHORT).show(); 68 } 69 protected void onPostExecute(String result) { 70 Toast.makeText(UserMessage.this, result, Toast.LENGTH_SHORT).show(); 71 } 72 } 73 }
下面是服务器端的代码:
1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 request.setCharacterEncoding("UTF-8"); 3 response.setContentType("text/html;charset=UTF-8"); 4 String mail = request.getParameter("mail"); // 用户提交的联系方式 5 String text = request.getParameter("text"); // 用户反馈的正文 6 String info = request.getParameter("info"); // 机型、应用版本号等信息 7 String results = writeData(mail, text, info) ? "提交成功" : "提交失败"; 8 response.getWriter().write(results); // 返回结果 9 } 10 public boolean writeData(String mail, String text, String info) { 11 String driver = "com.mysql.jdbc.Driver"; 12 String dbUrl = "jdbc:mysql://r.rdc.sae.sina.com.cn:3307/app_name"; 13 Connection connection = null; 14 PreparedStatement ps = null; 15 try { 16 Class.forName(driver);// 加载 17 // 连接数据库 18 connection = DriverManager.getConnection(dbUrl, SaeUserInfo.getAccessKey(), SaeUserInfo.getSecretKey()); 19 // 获取当前的时间,一起写入数据库 20 String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 21 String sql = "insert into feedback (mail, text, time, info) values (?, ?, ?, ?)"; 22 ps = connection.prepareStatement(sql); 23 ps.setString(1, mail); 24 ps.setString(2, text); 25 ps.setString(3, date); 26 ps.setString(4, info); 27 ps.execute(); 28 return true; 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 return false; 32 } catch (ClassNotFoundException e) { 33 e.printStackTrace(); 34 return false; 35 } finally { 36 try { 37 if (ps != null) { 38 ps.close(); 39 } 40 if (connection != null) { 41 connection.close(); 42 } 43 } catch (SQLException e) { 44 e.printStackTrace(); 45 } 46 } 47 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。