需要Internet权限,AndroidManifest.xml

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

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="id:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/input_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="99"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:text="name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试名称"/>
</LinearLayout>
<Button
android:id="@+id/btn01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送Post请求" /> <TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="获取图片" />
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
private TextView info=null;
private Button btn01=null;
private static final String strURL="http://192.168.1.2/android.ashx";
private boolean flag=false; //判断是否成功 private EditText input_id=null;
private EditText input_name=null; private Button btn02=null;
private ImageView img=null;
private static final String imgURL="http://www.baidu.com/img/bdlogo.gif";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.input_id=(EditText)super.findViewById(R.id.input_id);
this.input_name=(EditText)super.findViewById(R.id.input_name);
this.info=(TextView)super.findViewById(R.id.info);
this.btn01=(Button)super.findViewById(R.id.btn01);
this.btn01.setOnClickListener(new OnClickListenerimpl()); this.img=(ImageView)super.findViewById(R.id.img);
this.btn02=(Button)super.findViewById(R.id.btn02);
this.btn02.setOnClickListener(new OnClickListenerimpl());
} private class OnClickListenerimpl implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn01:
try {
HttpPost request=new HttpPost(strURL);
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", input_id.getText().toString()));
params.add(new BasicNameValuePair("name", input_name.getText().toString()));
request.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse response=new DefaultHttpClient().execute(request);
if(response.getStatusLine().getStatusCode()!=404){
//flag=Boolean.parseBoolean(EntityUtils.toString(response.getEntity()).trim());
MainActivity.this.info.setText(EntityUtils.toString(response.getEntity()).trim());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} break;
case R.id.btn02:
System.out.println(">>Btn02");
try {
byte data[] = MainActivity.this.getUrlData();
System.out.println(">>data_length:"+data.length);
Bitmap bmp=BitmapFactory.decodeByteArray(data, 0, data.length); //二进制到图片
MainActivity.this.img.setImageBitmap(bmp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
} }
public byte[] getUrlData() throws Exception{
ByteArrayOutputStream bos=null; //内存操作流
try {
URL url=new URL(imgURL);
bos=new ByteArrayOutputStream();
byte data[] =new byte[1024];
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
InputStream input=conn.getInputStream();
int len =0;
while ((len=input.read(data))!=-1) {
bos.write(data,0,len);
}
return bos.toByteArray();
} catch (Exception e) {
throw e;
} finally{
if(bos!=null){
bos.close();
}
}
}

使用C#的一般处理程序来接收Post来的信息,然后输出的内容会在Android程序上显示

android.ashx.cs

        public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int id = string.IsNullOrEmpty(context.Request.Form["id"]) ? : Convert.ToInt32(context.Request.Form["id"]);
string name = string.IsNullOrEmpty(context.Request.Form["name"]) ? "null" : context.Request.Form["name"].ToString();
if (id != && name != "null")
{
context.Response.Write(string.Format("传递参数的id是{0},传递参数的用户名是{1}", id, name));
}
else
{
context.Response.Write("参数错误");
} }

Android Post方式发送信息和获取URL中的图片的更多相关文章

  1. Android Get方式发送信息

    程序需要用到Internet权限,所以需要在AndroidManifest.xml添加 <uses-permission android:name="android.permissio ...

  2. 获取url中的参数\+发送ajax请求根路径|+获取复选框的值

    //获取url中的参数function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=( ...

  3. 记录一次bug解决过程:velocity中获取url中的参数

    一.总结 在Webx的Velocity中获取url中参数:$rundata.getRequest().getParameter('userId') 在Webx项目中,防止CSRF攻击(Cross-si ...

  4. 关于获取URL中传值的解决方法

    在我们页面的URL中包含着很多信息,包括域名,协议等等这里就不一一介绍了),对于我们开发者而言,使用比较多的就是页面之间的传值.为什么要页面传值呢?很简单,当你在浏览一个商品页面的时候你要看到一个商品 ...

  5. 如何获取url中的参数并传递给iframe中的报表

    在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...

  6. js获取URL中的参数

    js获取URL中的一些参数的意思 location对象 含有当前URL的信息. 属性 href 整个URL字符串. protocol 含有URL第一部分的字符串,如http: host 包含有URL中 ...

  7. APPCAN开发笔记:html页面之间的参数传递:使用js获取url中的参数,以及在APPCAN中不能使用的解决方法

    用PHP的GET/POST方式来传递方式已经是司空见惯了,但是如果我的页面是一个静态的html的页面,想传递参数的时候要怎么办呢?在APPCAN的开发中我们会经常遇到这样的问题,因为所有的页面都是静态 ...

  8. vue中如何不通过路由直接获取url中的参数

    前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...

  9. 关于获取URL中传值的解决方法--升级版

    这次页面之间的传值是升级版本,为什么是升级版本呢,因为这次页面的传值不一样了.大家可以看一下我原来的文章<关于获取URL中传值的解决方法> 其实上次就已经比较清楚的介绍了页面之间的传值,但 ...

随机推荐

  1. 【源码阅读】Java集合之三 - ArrayDeque源码深度解读

    Java 源码阅读的第一步是Collection框架源码,这也是面试基础中的基础: 针对Collection的源码阅读写一个系列的文章,本文是第三篇ArrayDeque. ---@pdai JDK版本 ...

  2. Shiro的Subject和Sessoin的创建

    之前要先了解Session的来源Shiro session和Spring session一样吗? 创建Subject的位置 AbstractShiroFilter . doFilterInternal ...

  3. 【302】C# TreeView 控件使用说明

    参考:C# 中treeview 树节点图标的动态加载,及选中时图标改变 参考:C# TreeView 控件的综合使用方法 参考:TreeView 类 参考:TreeNode 类 1. 添加根和子级 通 ...

  4. NSTimer类的使用

    转载于:http://www.cnblogs.com/wujian1360/archive/2011/09/05/2167992.html 创建一个 Timer + scheduledTimerWit ...

  5. UISprite

    [UISprite] UISprite用于引用一个UIAtlas中的sprite. 此脚本最强大的功能是Sprite Type,主要指定在需拉伸图片时的行为,它有以下几种值: 1)Simple:普通拉 ...

  6. 高性能Web服务器Nginx的配置与部署研究(8)核心模块之事件模块

    一.事件模块的作用是什么? 用来设置Nginx处理链接请求. 二.相关指令 1. accept_mutex 含义:设置是否使用连接互斥锁进行顺序的accept()系统调用. 语法:accept_mut ...

  7. HoloLens的显示分辨率有多少?

    作者:胡痴儿2.0链接:https://www.zhihu.com/question/27737626/answer/92339648来源:知乎著作权归作者所有,转载请联系作者获得授权. 左右眼各,7 ...

  8. Jquery循环select标签,并给指定option添加select属性后在页面上不显示的问题

    <select id="testId"> <option value="">--请选择--</option> <opt ...

  9. hive1.2.1问题集锦

    1.启动hive报错: Logging initialized using configuration in jar:file:/usr/local/hive-1.2.1/lib/hive-commo ...

  10. cacti监控mssql 2005运行资源情况

    概述:SQL Server2000\2005\2008本身不支持snmp,使用cacti监控mssql,必须通过php连接mssql来获取SQL 2005性能计算器的值. 操作步骤: 1.php连接m ...