需要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. nginx启动、重启、重新加载配置文件和平滑升级

    Nginx有一个主进程和几个工作进程,主进程的主要作用就是读取.评估配置文件和管理工作进程,工作进程对请求做实际处理.工作进程的数量是在配置文件中配置的,一般设置为cpu的核心数*线程数. nginx ...

  2. jstl格式化页面显示科学计数法问题

    <td content="planEnergy"><fmt:formatNumber value="${gg.planEnergy}" pat ...

  3. [poj2104]kth-number(归并树求区间第k大)

    复杂度:$O(nlog^3n)$ #include<cstdio> #include<cstring> #include<algorithm> #include&l ...

  4. 【总结整理】AI产品经理大会2017(转载)

    从企业大数据到企业 AI | 易观智慧院院长 李智 1.AI 不是目的,而是要了解 AI 是什么,真正意义上的强人工智能在前沿领域尚未取得突破,暂时只能在影视文学作品中去思考人机关系.机器人三定律在未 ...

  5. Dell 1420N使用Kubuntu默认无线驱动后网络不稳定的解决方法

    前几天在我的Dell 1420N上安装了Kubuntu 13.04,装了系统软件中的私有无线网卡驱动Broadcom STA wireless driver后,虽然能上网,但是很不稳定,经常断线,非常 ...

  6. Python爬虫入门四之Urllib库的高级用法

    1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...

  7. mac安装mysql及终端操作mysql与pycharm的数据库可视化

    一.Mac安装mysql 首先下载mysql,地址:https://dev.mysql.com/downloads/mysql/ 然后已知安装就好了,会出现让你记住密码的提示,然后就安装好了.... ...

  8. REDIS与MEMCACHED的区别(转)

    出处:http://www.blogjava.net/paulwong/archive/2013/09/06/403746.html 如果简单地比较Redis与Memcached的区别,大多数都会得到 ...

  9. js总结33 :javascript-DOM节点属性

    1 设置节点属性三个方法: 获取:getAttribute(名称) 设置:setAttribute(名称, 值) 删除:removeAttribute(名称) 举个例子: <!DOCTYPE h ...

  10. C/C++语言中指针数组和数组指针比较区别

    指针数组:int *p[3] 定义一个指针数组,其中每个数组元素指向一个int型变量的地址 意思就是指针的数组,数组里面都是指针 例子: int *p[3];//定义了一个指针数组,有3个成员,每个成 ...