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中传值的解决方法> 其实上次就已经比较清楚的介绍了页面之间的传值,但 ...
随机推荐
- Redis实战——redis主从备份和哨兵模式实践
借鉴:http://redis.majunwei.com/topics/sentinel.html https://blog.csdn.net/u011784767/article/detai ...
- a.call(b); call 方法
a.call(b); a.apply(b,[]) function class1() { this.name = function(){ alert("class1的方法name()&quo ...
- 【313】python 中 print 函数用法总结
参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...
- 关于启动MongDB的mongod.exe文件闪退的问题
昨天学mongdb的时候,遇到了mongod.exe闪退的问题,解决办法很简单: 你可以不执行mongod.exe,直接用命令行操作 在你安装mongdb的盘的根目录下创建一个data文件夹,一定要在 ...
- 自动补齐flexselect+级联下拉框案例
在开发web应用时,经常遇到类似省市区级联下拉框操作,即选中省份自动级联加载该省份所有的市,选中市自动级联加载该市所有的区:假设省市区的数据量很大,此时用户想选中某市,因而要从上往下查找,可能半天都找 ...
- tomcat https
转自 http://11lingxian.iteye.com/blog/1491607 双向认证: 客户端向服务器发送消息,首先把消息用客户端证书加密然后连同时把客户端证书一起发送到服务器端, 服务器 ...
- Struts2常量详解
-----------------siwuxie095 Struts2 常量详解 Struts2 的常量大多在默认的配置文件中已经配置好,但根据 用户需求和开发要求的不同,可能需要修改这些常量值,修改 ...
- ajax获取json数据为undefined--原因解析
解决办法:var dataObj=eval("("+data+")");//转换为json对象 问题: 1. 碰到一个问题ajax成功获取json数据后,取值显 ...
- linux常用的一些命令行操作(ubuntu)
软件安装 sudo apt-get install xxx 压缩和解压缩 1. *.tar 用 tar –xvf 解压 2. *.gz 用 gzip -d或者gunzip 解压 3. *.tar.gz ...
- maven filter插件只替换了部分变量问题
maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...