android http-post方法简单实现

tankeryang 贡献于2015-05-13

作者   创建于2012-06-06 01:30:27   修改者  修改于2013-09-04 05:43:07字数2706

文档摘要:本代码演示在 Android 如何使用 POST 来提交数据
关键词:

本文由www.microoh.com整理,转载请注明出处 android http-post方法简单实现 本代码演示在 Android 如何使用 POST 来提交数据 Java代码 1 packagecom.hl; 2 3 importjava.io.BufferedReader; 4 importjava.io.IOException; 5 importjava.io.InputStream; 6 importjava.io.InputStreamReader; 7 importjava.util.ArrayList; 8 importjava.util.HashMap; 9 importjava.util.Iterator; 10 importjava.util.Map; 11 importjava.util.Set; 12 13 importorg.apache.http.HttpEntity; 14 importorg.apache.http.HttpResponse; 15 importorg.apache.http.client.entity.UrlEncodedFormEntity; 16 importorg.apache.http.client.methods.HttpPost; 17 importorg.apache.http.impl.client.DefaultHttpClient; 18 importorg.apache.http.message.BasicNameValuePair; 19 20 importandroid.app.Activity; 21 importandroid.os.Bundle; 22 importandroid.view.View; 23 importandroid.view.View.OnClickListener; 24 importandroid.widget.Button; 25 importandroid.widget.EditText; 26 importandroid.widget.TextView; 27 28 publicclassSimplePOSTextendsActivity{ 29 privateTextViewshow; 30 privateEditTexttxt; 31 privateButtonbtn; 32 33 @Override 34 publicvoidonCreate(BundlesavedInstanceState){ 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.main); 37 show=(TextView)findViewById(R.id.show); 38 txt=(EditText)findViewById(R.id.txt); 39 btn=(Button)findViewById(R.id.btn); 40 btn.setOnClickListener(newOnClickListener(){ 本文由www.microoh.com整理,转载请注明出处 41 @Override 42 publicvoidonClick(Viewv){ 43 dopost(txt.getText().toString()); 44 45 } 46 }); 47 } 48 49 privatevoiddopost(Stringval){ 50 //封装数据 51 Mapparmas=newHashMap(); 52 parmas.put("name",val); 53 54 DefaultHttpClientclient=newDefaultHttpClient();//http客户端 55 HttpPosthttpPost=newHttpPost("http://mhycoe.com/test/post.php"); 56 57 ArrayListpairs=newArrayList(); 58 if(parmas!=null){ 59 Setkeys=parmas.keySet(); 60 for(Iteratori=keys.iterator();i.hasNext();){ 61 Stringkey=(String)i.next(); 62 pairs.add(newBasicNameValuePair(key,parmas.get(key))); 63 } 64 } 65 66 try{ 67 UrlEncodedFormEntityp_entity=newUrlEncodedFormEntity(pairs,"utf-8"); 68 /* 69 *将POST数据放入HTTP请求 70 */ 71 httpPost.setEntity(p_entity); 72 /* 73 *发出实际的HTTPPOST请求 74 */ 75 HttpResponseresponse=client.execute(httpPost); 76 HttpEntityentity=response.getEntity(); 77 InputStreamcontent=entity.getContent(); 78 StringreturnConnection=convertStreamToString(content); 79 show.setText(returnConnection); 80 }catch(IllegalStateExceptione){ 81 e.printStackTrace(); 82 }catch(IOExceptione){ 83 e.printStackTrace(); 84 } 本文由www.microoh.com整理,转载请注明出处 85 86 } 87 88 privateStringconvertStreamToString(InputStreamis){ 89 BufferedReaderreader=newBufferedReader(newInputStreamReader(is)); 90 StringBuildersb=newStringBuilder(); 91 Stringline=null; 92 try{ 93 while((line=reader.readLine())!=null){ 94 sb.append(line); 95 } 96 }catch(IOExceptione){ 97 e.printStackTrace(); 98 }finally{ 99 try{ 100 is.close(); 101 }catch(IOExceptione){ 102 e.printStackTrace(); 103 } 104 } 105 returnsb.toString(); 106 } 107 }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 2 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档