android_27_get请求提交数据_HttpURLConnection
来源:互联网 发布:卷积矩阵及其运算实例 编辑:程序博客网 时间:2024/05/16 05:22
效果图:
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 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>"; } } // 录入到where表格中,并且返回另一半的地址 public function insertwithpost(){ $language = $_POST["language"]; $content = $_POST["content"]; // 可空 $contact = $_POST["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.getrequest" 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=".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.getrequest.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.getrequest;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.getrequest;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;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();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic 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;}@Overridepublic 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() {@Overridepublic void run() {// 提交的数据需要经过url编码,英文和数字编码后不变@SuppressWarnings("deprecation")String path = "http://sg31.com/ci/androidfeedback/insertwithget?language=android&content="+ URLEncoder.encode(content) + "&contact=" + contact;try {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);conn.setReadTimeout(5000);System.out.println("sg__"+conn.getResponseCode());if (conn.getResponseCode() == 200) {InputStream is = conn.getInputStream();String text = TextHelper.textFromStream(is);System.out.println("sg__"+text);Message msg = handler.obtainMessage();msg.obj = text;handler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();}}};t.start();}}
1 0
- android_27_get请求提交数据_HttpURLConnection
- android_29_post请求_HttpURLConnection
- 赵雅智_HttpURLConnection 的 get请求和post请求
- 表单提交,数据请求
- 防止jsp数据请求重复提交
- php curl模拟post请求提交数据
- Post方式请求提交数据到服务器
- 表单提交数据get请求和post请求的区别
- 表单提交数据get请求和post请求的区别
- 表单提交数据get请求和post请求的区别
- 表单提交数据get请求和post请求的区别?
- 提交请求
- servlet获取表单提交的数据、请求转发、请求包含、请求重定向
- Android-上传图片(-)_HttpURLConnection
- 464_HttpURLConnection的post
- PHP CURL 模拟POST请求 提交数据或上传文件
- Android普通方式的DoPost请求提交数据
- 表单提交数据请求 get和post的 区别
- git reset 命令
- spring mvc接受前端投递的json问题
- Linux 常用命令详解链接
- 关于JAVA小程序完整打包过程
- suse上的一些简单操作
- android_27_get请求提交数据_HttpURLConnection
- HTTP状态码
- HTTP Request
- 2016年全年总结
- Myeclipse 快捷键大全(绝对全)
- 纯c语言实现链表,实现链表增删改查
- servlet的url-pattern匹配规则
- xp系统升级win7系统教程
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile