[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 ...
随机推荐
- Python自动化 【第五篇】:Python基础-常用模块
目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...
- Ninject的使用
摘要 DI容器的一个责任是管理他创建的对象的生命周期.他应该决定什么时候创建一个给定类型的对象,什么时候使用已经存在的对象.他还需要在对象不需要的时候处理对象.Ninject在不同的情况下管理对象的生 ...
- HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- Windows下使用性能监视器监控SqlServer的常见指标
这篇文章主要介绍了Windows下使用性能监视器监控SqlServer的常见指标,常见指标包括Buffer Cache Hit Ratio.Pages/sec. Available Bytes.Dis ...
- Prefab强制使用文本模式
[Prefab强制使用文本模式] Edit -> ProjectSetting -> Editor:
- Raspberry Pi(树莓派)国内软件源
树莓派自带的软件源是 deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi 由于网站在国外 ...
- 初识ASP.NET CORE:二、优劣
Which one is right for me? ASP.NET is a mature web platform that provides all the services that you ...
- Cannot assign to 'self' outside of a method in the init family
今天在重写父类的init方法时报错如下: error:Cannot assign to 'self' outside of a method in the init family 这种问题以前从来没有 ...
- cmd.ExecuteReader(CommandBehavior.CloseConnection)
有些开发人员坚持认为,如果您设置 CommandBehavior.CloseConnection 选项,则 DataReader 及其相关联的连接会在 DataReader 完成数据读取时自动关闭.这 ...
- python 小知识
PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 使用下面的代码可以打印PYTHONPATH: print(os.sys.path) impr ...