最近在搞安卓,跟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" %>
再來就是C# 得部分:

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]);

}
      }
    }
 }

 
再來就是Android 的部分:

//傳送資料回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:'許'}");

結果:

注意事項:
1.其中傳輸我都是透過格式為UTF-8
2.網路的Permission 記得打開  需要在 AndroidManifest.xml 中加入

<uses-permission android:name="android.permission.INTERNET"/>

3.還有一個地方需再Activity 中加入:

// AllowPolicy StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

[C#] 與Android共舞–手機post資料給Server (转帖)的更多相关文章

  1. [C#] 與Android共舞–透過GET方式傳資料給Server(含解決中文編碼問題) (转帖)

    上一篇文章分享了透過POST 方式傳資料回Server,這一篇來談談有關於透過GET的方式傳遞 首先,如我預期的一樣,透過網址傳遞,會產生編碼問題,這邊我就順代解掉,希望有碰到的人 可以不用為此煩惱. ...

  2. 手機 停充的種類 與 量測 power consumption 功率 使用 bq25896 bq25890

    Precondition : 配有 power path 功能的 BQ2589 手機. 接上 pc usb port. Origin : 今天有同事問我, 手機是否可以在接上 pc usb port ...

  3. 榮耀6 Plus將是一部沒有對手的手機

    華為榮耀官方微博發佈消息正式確定了年度旗艦新品將命名為榮耀6 Plus,據稱,該機將是“2014年度最最旗艦手機”,並將集“科學與美學一身”.“探索幾何與視覺極限”,同時,官方微博還不低調地宣稱該機將 ...

  4. iPhone6手機產品提交了進網申請

    近期,海外投資蘋果公司為iPhone6手機產品提交了進網申請,經電信設備進網檢測機構測試和我部審查,相關產品滿足進網管理要求.根據<電信條例>有關規定,我部依法定程式在法定時限內為蘋果公司 ...

  5. 透過手機 App 在 OpenELEC(XBMC)中輸入中文

    這裡介紹如何使用手機 App 在沒有中文輸入法的 OpenELEC(XBMC)中輸入中文字. OpenELEC(XBMC)雖然有內建中文語系,但是卻沒有中文的輸入法,沒辦法直接輸入中文字,這對於一般家 ...

  6. “双十二”年终盛典,Guitar Pro邀您一起倾情共舞

    躲过了双十一,躲不过双十二,隐约昨天还是双十一,马上双十二又叕来了,弱弱的问一句“你们的手长粗了来了吗?”在这即将结束的年终盛典里,各商家又将如“双十一”般纷纷使出浑身解数,作为吉他最佳拍档的编曲软件 ...

  7. 為什麼我的手機連Wi-Fi速度總是卡在75Mbps?Wi-Fi速度解惑~帶你一次看懂!

    正文字体大小:大 中 小 為什麼我的手機連Wi-Fi速度總是卡在75Mbps?Wi-Fi速度解惑-帶你一次看懂! (2017-02-21 10:57:48) 转载▼ 标签: wi-fi速度 手機wi- ...

  8. Palm是一家英國智能手機公司

    據TCL方面介紹,本次收購只涉及品牌,不會涉及員工和其他資產.被收購之後,Palm仍將繼續把總部設於美國加州矽谷,以發揮該區域所獨有的先進技術和人才的優勢. TCL通訊CEO郭愛平表示TCL將把Pal ...

  9. [转] 与调试器共舞 - LLDB 的华尔兹

    你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...

随机推荐

  1. PIC32MZ tutorial -- Timer Interrupt

    An interrupt is an internal or external event that requires quick attention from the controller. The ...

  2. projecteuler Problem 8 Largest product in a series

    The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = ...

  3. struts2下s:iterator取不出值

    1:问题起因:通过action方法连接数据库取出Arraylist集合,在前台页面遍历显示无数据,用s:debug查看 stack里有值,用了各种方法,包括session传值,但是仍然取不出来. 2: ...

  4. html text加提示语

    <input type="text" id="key" name="key" value=" 请输入关键词" on ...

  5. HTML <!DOCTYPE> Declaration

    <!DOCTYPE html><html><head><title>Title of the document</title></he ...

  6. Win7 64位系统 VS2010连接Oracle报错的问题

    1,异常现象: TNS无法识别 2,异常分析:VS2010启动调试的时候,出现异常.但是使用IIS作为服务器的时候,是不会出现异常,也就是可以正常访问到数据库,后来分析是由于VS自带服务器ASP.NE ...

  7. Linux程序包管理初步-yum的使用

    何为yum? yum:由yellow dog研发,全称Yellowdog Update Modifier 是rhel系列系统上rpm包管理器的前端工具,可用来自动解决依赖关系,从而更好的实现程序包的安 ...

  8. 使用IntelliJ IDEA建立java web项目

    一.“File”->"New Project"弹出对话框,选择“Java Enterprise” 二.选择web模板 三.输入项目名 四.项目结构

  9. plink:将bed文件转化为ped,map文件

    用--recode命令,--out表示转化的文件的名字,本例已经命名为“filter” /plink-1.07-x86_64/plink --bfile filter --recode --out f ...

  10. 关于jquery判断对象是否为空

    1. jquery对象分为两种,一种是dom对象,dom对象会自带一个length属性,所以这种情况: obj.length == 0 可以判断对象为空 2. jquery也可以自定义对象,如 var ...