Android HttpConnect
2010-10-19 20:17:34 来源:WEB开发网43. List < NameValuePair> params = new ArrayList< NameValuePair>();
44. params.add(new BasicNameValuePair("str", "i am post string"));
45.
46. try {
47. httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
48. HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
49. if (httpResponse.getStatusLine().getStatusCode() == 200) {
50. String strResult = EntityUtils.toString(httpResponse.getEntity());
51. mtv1.setText(strResult);
52. }
53. } catch (Exception e) {
54. // TODO: handle exception
55. e.printStackTrace();
56. }
57. }
58.
59. }
60. );
61.
62. mb2.setOnClickListener(new Button.OnClickListener() {
63.
64. public void onClick(View v) {
65. // TODO Auto-generated method stub
66. String uriAPI = "http://www.dubblogs.cc:8751/android/test/api/get/index.php?str=I+am+Get+String";
67. HttpGet httpRequest = new HttpGet(uriAPI);
68. try {
69. HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
70. if (httpResponse.getStatusLine().getStatusCode() == 200) {
71. String strResult = EntityUtils.toString(httpResponse.getEntity());
72. strResult = eregi_replace("( || | )", "", strResult);
73. mtv1.setText(strResult);
74. }
75. } catch (Exception e) {
76. // TODO: handle exception
77. e.printStackTrace();
78. }
79. }
80.
81. }
82. );
83. }
84.
85. protected String eregi_replace(String strFrom, String strTo,
86. String strTarget) {
87. // TODO Auto-generated method stub
88. String strPattern = "(?!)" + strFrom;
Tags:Android HttpConnect
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接