[C#] 與Android共舞–手機post資料給Server (转帖)
最近在搞安卓,跟Server溝通是一定要的,這範例很簡單,就是我在Android 上面,透過POST 的方式傳資料給
Server ,則Server 收到值後直接回傳, Server side 是用asp.net C# 寫作..
現在直接來看Code
Server 端(C#):
建立一個echo.aspx 在aspx 得部分除了第一行全部都拿掉
只剩下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="echo.aspx.cs" Inherits="EchoService.echo" %>
using System;
namespace EchoService
{
public partial class echo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form.Count == 0)
{
Response.Write("{}");
}else
{
Response.Write(Request.Form[0]);
}
}
}
}
//傳送資料回Server //urlPath: 後端網址 //data: 資料
public static String SendHttpPost(String urlPath, String data) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(urlPath);
httpPostRequest.setEntity(new StringEntity(data, HTTP.UTF_8));
httpPostRequest.setHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
httpPostRequest.setHeader("Accept-Encoding", "gzip");
HttpResponse response = (HttpResponse) httpclient
.execute(httpPostRequest);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Header contentEncoding = response
.getFirstHeader("Content-Encoding");
if (contentEncoding != null
&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
String resultString = convertStreamToString(instream);
return resultString;
}
} catch (Exception e) {
Log.e("WebUtil", e.toString());
}
return "";
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*
* (c) public domain:
* http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/
* 11/a-simple-restful-client-at-android/
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try { is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
呼叫方式:
String res = SendHttpPost( "http://swap.no2don.com/echo.aspx", "{name:'許當麻',first_name:'當麻',last_name:'許'}");
結果:
![]()
<uses-permission android:name="android.permission.INTERNET"/>
// AllowPolicy StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
[C#] 與Android共舞–手機post資料給Server (转帖)的更多相关文章
- [C#] 與Android共舞–透過GET方式傳資料給Server(含解決中文編碼問題) (转帖)
上一篇文章分享了透過POST 方式傳資料回Server,這一篇來談談有關於透過GET的方式傳遞 首先,如我預期的一樣,透過網址傳遞,會產生編碼問題,這邊我就順代解掉,希望有碰到的人 可以不用為此煩惱. ...
- 手機 停充的種類 與 量測 power consumption 功率 使用 bq25896 bq25890
Precondition : 配有 power path 功能的 BQ2589 手機. 接上 pc usb port. Origin : 今天有同事問我, 手機是否可以在接上 pc usb port ...
- 榮耀6 Plus將是一部沒有對手的手機
華為榮耀官方微博發佈消息正式確定了年度旗艦新品將命名為榮耀6 Plus,據稱,該機將是“2014年度最最旗艦手機”,並將集“科學與美學一身”.“探索幾何與視覺極限”,同時,官方微博還不低調地宣稱該機將 ...
- iPhone6手機產品提交了進網申請
近期,海外投資蘋果公司為iPhone6手機產品提交了進網申請,經電信設備進網檢測機構測試和我部審查,相關產品滿足進網管理要求.根據<電信條例>有關規定,我部依法定程式在法定時限內為蘋果公司 ...
- 透過手機 App 在 OpenELEC(XBMC)中輸入中文
這裡介紹如何使用手機 App 在沒有中文輸入法的 OpenELEC(XBMC)中輸入中文字. OpenELEC(XBMC)雖然有內建中文語系,但是卻沒有中文的輸入法,沒辦法直接輸入中文字,這對於一般家 ...
- “双十二”年终盛典,Guitar Pro邀您一起倾情共舞
躲过了双十一,躲不过双十二,隐约昨天还是双十一,马上双十二又叕来了,弱弱的问一句“你们的手长粗了来了吗?”在这即将结束的年终盛典里,各商家又将如“双十一”般纷纷使出浑身解数,作为吉他最佳拍档的编曲软件 ...
- 為什麼我的手機連Wi-Fi速度總是卡在75Mbps?Wi-Fi速度解惑~帶你一次看懂!
正文字体大小:大 中 小 為什麼我的手機連Wi-Fi速度總是卡在75Mbps?Wi-Fi速度解惑-帶你一次看懂! (2017-02-21 10:57:48) 转载▼ 标签: wi-fi速度 手機wi- ...
- Palm是一家英國智能手機公司
據TCL方面介紹,本次收購只涉及品牌,不會涉及員工和其他資產.被收購之後,Palm仍將繼續把總部設於美國加州矽谷,以發揮該區域所獨有的先進技術和人才的優勢. TCL通訊CEO郭愛平表示TCL將把Pal ...
- [转] 与调试器共舞 - LLDB 的华尔兹
你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...
随机推荐
- js事件委托,可以使新添加的元素具有事件(event运用)
miaov视频教程 http://study.163.com/course/courseMain.htm?courseId=231002 <!DOCTYPE html PUBLIC " ...
- SQL笔记-第四章,数据的检索
一.select的简单用法 1.简单的数据检索 SELECT * FROM T_Employee; 2.检索出需要的列 SELECT FNumber,FName,FAge FROM T_Employe ...
- 利用jquery实现自动登录
前提是需要引入jquery和jquery.cookie html 用户名:<input type="text" id="name"/><br ...
- 无法连接到已配置的开发web服务器
http://jingyan.baidu.com/article/29697b91099847ab20de3c8b.html 这是防火墙造成的,将防火墙关闭即可
- javascript generate a guid
function Guid() { var random = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); retu ...
- 快速升级php5.6
!!yum list installed | grep phpcd /etc/yum.repos.drpm -Uvh https://mirror.webtatic.com/yum/el6/lates ...
- C#去掉list集合中的重复数据
List<string> conList= new List<string>(); List<string> listII = new List<string ...
- hdu 1797 靠谱的算法应该是最大生成树,但是本人用最大流做的
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 22294 Accepted: ...
- SQL Server数据库表重置自增主键号(通常是指ID)
执行 DBCC CHECKIDENT ('table_name', NORESEED) 以确定列中的当前最大值 然后使用 DBCC CHECKIDENT ('table_name', RESEED,n ...
- visio形状内文本的换行符
& Chr() &Shape sp;sp.Characters = "12345" & Chr(10) & "56789"; 注 ...