android http-post方法简单实现

android技术开发 发布于 2013/08/21 17:31
阅读 107
收藏 0

android http-post方法简单实现

本代码演示在 Android 如何使用 POST 来提交数据

Java代码 

1 packagecom.hl; 

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(){ 

41 

42 @Override

43 publicvoidonClick(Viewv){ 

44 dopost(txt.getText().toString()); 

45 

46 } 

47 }); 

48 } 

49 

50 privatevoiddopost(Stringval){ 

51 //封装数据 

52 Map<String,String>parmas=newHashMap<String,String>(); 

53 parmas.put("name",val); 

54 

55 DefaultHttpClientclient=newDefaultHttpClient();//http客户端 

56 HttpPosthttpPost=newHttpPost("http://mhycoe.com/test/post.php"); 

57 

58 ArrayList<BasicNameValuePair>pairs=newArrayList<BasicNameValuePair>(); 

59 if(parmas!=null){ 

60 Set<String>keys=parmas.keySet(); 

61 for(Iterator<String>i=keys.iterator();i.hasNext();){ 

62 Stringkey=(String)i.next(); 

63 pairs.add(newBasicNameValuePair(key,parmas.get(key))); 

64 } 

65 } 

66 

67 try{ 

68 UrlEncodedFormEntityp_entity=newUrlEncodedFormEntity(pairs,"utf-8"); 

69 /* 

70 *POST数据放入HTTP请求 

71 */

72 httpPost.setEntity(p_entity); 

73 /* 

74 *发出实际的HTTPPOST请求 

75 */

76 HttpResponseresponse=client.execute(httpPost); 

77 HttpEntityentity=response.getEntity(); 

78 InputStreamcontent=entity.getContent(); 

79 StringreturnConnection=convertStreamToString(content); 

80 show.setText(returnConnection); 

81 }catch(IllegalStateExceptione){ 

82 e.printStackTrace(); 

83 }catch(IOExceptione){ 

84 e.printStackTrace(); 

85 } 

86 

87 } 

88 

89 privateStringconvertStreamToString(InputStreamis){ 

90 BufferedReaderreader=newBufferedReader(newInputStreamReader(is)); 

91 StringBuildersb=newStringBuilder(); 

92 Stringline=null; 

93 try{ 

94 while((line=reader.readLine())!=null){ 

95 sb.append(line); 

96 } 

97 }catch(IOExceptione){ 

98 e.printStackTrace(); 

99 }finally{ 

100 try{ 

101 is.close(); 

102 }catch(IOExceptione){ 

103 e.printStackTrace(); 

104 } 

105 } 

106 returnsb.toString(); 

107 } 

108 }



加载中
返回顶部
顶部