Android Post方式发送信息和获取URL中的图片
需要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中的图片的更多相关文章
- Android Get方式发送信息
程序需要用到Internet权限,所以需要在AndroidManifest.xml添加 <uses-permission android:name="android.permissio ...
- 获取url中的参数\+发送ajax请求根路径|+获取复选框的值
//获取url中的参数function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=( ...
- 记录一次bug解决过程:velocity中获取url中的参数
一.总结 在Webx的Velocity中获取url中参数:$rundata.getRequest().getParameter('userId') 在Webx项目中,防止CSRF攻击(Cross-si ...
- 关于获取URL中传值的解决方法
在我们页面的URL中包含着很多信息,包括域名,协议等等这里就不一一介绍了),对于我们开发者而言,使用比较多的就是页面之间的传值.为什么要页面传值呢?很简单,当你在浏览一个商品页面的时候你要看到一个商品 ...
- 如何获取url中的参数并传递给iframe中的报表
在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数 ...
- js获取URL中的参数
js获取URL中的一些参数的意思 location对象 含有当前URL的信息. 属性 href 整个URL字符串. protocol 含有URL第一部分的字符串,如http: host 包含有URL中 ...
- APPCAN开发笔记:html页面之间的参数传递:使用js获取url中的参数,以及在APPCAN中不能使用的解决方法
用PHP的GET/POST方式来传递方式已经是司空见惯了,但是如果我的页面是一个静态的html的页面,想传递参数的时候要怎么办呢?在APPCAN的开发中我们会经常遇到这样的问题,因为所有的页面都是静态 ...
- vue中如何不通过路由直接获取url中的参数
前言:为什么要不通过路由直接获取url中的参数? vue中使用路由的方式设置url参数,但是这种方式必须要在路径中附带参数,而且这个参数是需要在vue的路由中提前设置好的. 相对来说,在某些情况下直接 ...
- 关于获取URL中传值的解决方法--升级版
这次页面之间的传值是升级版本,为什么是升级版本呢,因为这次页面的传值不一样了.大家可以看一下我原来的文章<关于获取URL中传值的解决方法> 其实上次就已经比较清楚的介绍了页面之间的传值,但 ...
随机推荐
- application-defined exception
dataSnap服务器,客户端调用的时候写错了一句话, SQLConnection1->CloneConnection(); 改为 SQLConnection1->Close(); 就好了 ...
- npm 全局环境变量配置
我们要先配置npm的全局模块的存放路径以及cache的路径,例如我希望将以上两个文件夹放在NodeJS的主目录下,便在NodeJs下建立”node_global”及”node_cache”两个文件 ...
- (转存 作者未知)深入理解HTML协议
深入理解HTML协议 http协议学 习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(Worl ...
- 前端生成二维码 - Javascript生成二维码(QR)
前段时间项目中需要动态的生成二维码,经过评估,前后端生成都可以.但后端生成会有两个问题: 没有找到正规发布出来的后端开源库. 二维码图片,会随着商品的增加而不断变多. 基于以上两个问题,决定在前端生成 ...
- C#给图片加文字和图片的水印
/// <summary> /// WaterMark 的摘要说明 /// </summary> /// 图片加水印 /// <param name="strC ...
- 一个word小技巧
最近在进行word格式重拍的时候发现了一个有些恶心的事,怎么去匹配文档里面所有的中文呢? 后来通过网络搜索发现了答案,在word中的查找和替换中有一个选项,可以使用通配符进行匹配. 当我们使用 ([一 ...
- mybatis中使用懒加载实现一对多复杂查询
1.包结构 2.pom配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...
- Centos7 安装Nginx服务
第一种方式:通过yum安装 直接通过 yum install nginx 肯定是不行的,因为yum没有nginx,所以首先把 nginx 的源加入 yum 中. 运行下面的命令: 1.将nginx放到 ...
- Nginx概述、安装及配置详解
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- Git回滚到历史节点(SourceTree篇)
转自:http://blog.csdn.net/u010416101/article/details/78142697.https://www.zhihu.com/question/48178380 ...