效果:
PHP代码:(采用CI框架)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// header("Content-Type: text/html; charset=utf-8");
class Androidfeedback extends CI_Controller {
// 录入到where表格中,并且返回另一半的地址
public function insertwithpost(){
$language = $_POST["language"];
echo "language";
$content = $_POST["content"];
echo "content";
// 可空
$contact = $_POST["contact"];
echo "contact";
$ipaddress = $_SERVER["REMOTE_ADDR"];
echo "ipaddress";
$servername = $_SERVER['SERVER_NAME'];
echo "servername";
// if ($servername != "sg31.com") {
// # code...
// echo "非法的访问";
// return;
// }
// 时间戳
$timeStamp = time();
if (strlen($content) > 250 || strlen($contact) > 250 || strlen($language) > 250) {
# code...
echo "字数不得超过255";
return;
}
if (strlen($content) == 0 || strlen($language) == 0) {
# code...
echo "妳还没有输入内容";
return;
}
if (strlen($contact) == 0) {
# code...
$contact = "sg31.com";
}
// 成功则插入,返回ok
// 插入 (REMOTE_ADDR)
// 时间戳
$pubtime = time();
$pubdate = date("Y-m-d");
$sql =
'INSERT INTO sg_app_feedback(language,content,contact,pubtime,ipaddress,pubdate)
VALUES(?,?,?,?,?,?)';
// $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$B = $this->db->query($sql, array($language,$content,$contact,$pubtime,$ipaddress,$pubdate));
if ($B == 1) {
# code...
// 插入成功,返回ok
echo "<h1>We are glad to hear from you:".$ipaddress."--".$servername."!</h1><h1>Thanks for your suggestion!</h1><h1>Best wishes for you!</h1>";
}
}
// 录入到where表格中,并且返回另一半的地址
public function insertwithget(){
$language = $_GET["language"];
$content = $_GET["content"];
// 可空
$contact = $_GET["contact"];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$servername = $_SERVER['SERVER_NAME'];
// if ($servername != "sg31.com") {
// # code...
// echo "非法的访问";
// return;
// }
// 时间戳
$timeStamp = time();
if (strlen($content) > 250 || strlen($contact) > 250 || strlen($language) > 250) {
# code...
echo "字数不得超过255";
return;
}
if (strlen($content) == 0 || strlen($language) == 0) {
# code...
echo "妳还没有输入内容";
return;
}
if (strlen($contact) == 0) {
# code...
$contact = "sg31.com";
}
// 成功则插入,返回ok
// 插入 (REMOTE_ADDR)
// 时间戳
$pubtime = time();
$pubdate = date("Y-m-d");
$sql =
'INSERT INTO sg_app_feedback(language,content,contact,pubtime,ipaddress,pubdate)
VALUES(?,?,?,?,?,?)';
// $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$B = $this->db->query($sql, array($language,$content,$contact,$pubtime,$ipaddress,$pubdate));
if ($B == 1) {
# code...
// 插入成功,返回ok
echo "<h1>We are glad to hear from you:".$ipaddress."--".$servername."!</h1><h1>Thanks for your suggestion!</h1><h1>Best wishes for you!</h1>";
}
}
}
/* End of file log.php */
/* Location: ./application/controllers/log.php */
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sg31.postrequest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
// 注意权限
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sg31.postrequest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sg31.postrequest.MainActivity"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入反馈意见或建议"
android:text="妳的app有个大bug!!!"
/>
<EditText
android:id="@+id/et_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请留下妳的联系方式(可空)"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:onClick="commitBtnClicked"
/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tv_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
</LinearLayout>
帮助类:
package com.sg31.postrequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class TextHelper {
public static String textFromStream(InputStream is){
byte[] b = new byte[1024];
int len = 0;
//创建字节数组输出流,读取输入流的文本数据时,同步把数据写入数组输出流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while((len = is.read(b)) != -1){
bos.write(b, 0, len);
}
//把字节数组输出流里的数据转换成字节数组
String text = new String(bos.toByteArray());
return text;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
主控制器:
package com.sg31.postrequest;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import com.sg31.postrequest.R;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
Toast.makeText(MainActivity.this, (String) msg.obj, 0).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void commitBtnClicked(View v) {
EditText et_content = (EditText) findViewById(R.id.et_content);
EditText et_contact = (EditText) findViewById(R.id.et_contact);
final String content = et_content.getText().toString();
final String contact = et_contact.getText().toString();
Thread t = new Thread() {
@Override
public void run() {
// 提交的数据需要经过url编码,英文和数字编码后不变
@SuppressWarnings("deprecation")
String path = "http://sg31.com/ci/androidfeedback/insertwithpost";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
// 拼接出要提交的数据的字符串
String data = "content=" + URLEncoder.encode(content)
+ "&contact=" + contact + "&language=" + "android";
// 死记: 添加post请求的两行属性
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length()
+ "");
// 设置打开输出流
conn.setDoOutput(true);
// 拿到输出流
OutputStream os = conn.getOutputStream();
// 使用输出流往服务器提交数据
os.write(data.getBytes());
System.out.println("sg__" + conn.getResponseCode());
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
String text = TextHelper.textFromStream(is);
Message msg = handler.obtainMessage();
msg.obj = text;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
}