需要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. 谈谈跨平台的app开发 坚定的选择了flutter

    目前市场上,(市场也许用的不对),比较常见的技术有xamrin.RN.Flutter,确切的说flutter是后起之秀,笔者也是最近才开始学习,xamrin是微软系的技术,笔者也很早就开始学习了,RN ...

  2. mongodb 的操作

    查找 db.collection的名字.find({"group":"kkkk","key":{$regex:/.*one.*/i}}); ...

  3. 从零玩转JavaWeb系列7web服务器-----get与post的区别

    总结get与post的区别 get参数通过url传递,post放在request body中. get请求在url中传递的参数是有长度限制的,而post没有. get比post更不安全,因为参数直接暴 ...

  4. 使用OpenSsl自己CA根证书,二级根证书和颁发证书(亲测步骤)

    ---恢复内容开始--- 一.介绍 企业自用, 到证书机构签发证书的费用和时间等都可以省下..... SSl证书的背景功用.......(省略万字,不废话) 可以参考: SSL证书_百度百科 X509 ...

  5. Robocopy和xxcopy全掌握

    Windows提供的复制操作功能实在是太过简陋,可定制性又不强,在复制.移动.备份文件夹的时候,总要循环往复做多次操作.现在我们就向你介绍两款强力复制备份软件:Robocopy和XXCOPY,具体功能 ...

  6. java基础之HashSet如何保证对象的唯一性

    首先Set集合是无序的 不可重复的 add的时候判断对象是否重复是用的equals HashSet<String> 存储String类型的数据时是可以保证数据的唯一性的 因为String类 ...

  7. mysql-5.6.24-win32解决没有my.ini并且修改编码

    3.配置环境变量:新建一个系统变量: MYSQL_HOME, 值:D:\MySql\mysql5611  //这一步不做也行 4.修改MySql启动配置文件: 将安装目录下文件:my-default. ...

  8. iOS中wkwebview加载本地html的要点

    项目中有些页面,我采用了html页面开发,然后用wkwebview加载的设计.在加载过程中遇见了一些问题,在这里进行一些记载和讨论.如有不同意见欢迎进行评论沟通. 问题时候这样的: 在webview的 ...

  9. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  10. sudo问题汇总

    1. 注释Defaults requiretty Defaults requiretty修改为 #Defaults requiretty, 表示不需要控制终端. 否则会出现sudo: sorry, y ...