需要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"]) ? 0 : Convert.ToInt32(context.Request.Form["id"]); string name = string.IsNullOrEmpty(context.Request.Form["name"]) ? "null" : context.Request.Form["name"].ToString(); if (id != 0 && name != "null") { context.Response.Write(string.Format("传递参数的id是{0},传递参数的用户名是{1}", id, name)); } else { context.Response.Write("参数错误"); } }