1 packagecom.example.httpdemo2;2

3 importjava.io.BufferedReader;4 importjava.io.IOException;5 importjava.io.InputStream;6 importjava.io.InputStreamReader;7 importjava.io.UnsupportedEncodingException;8 importjava.util.ArrayList;9 importjava.util.List;10

11 importorg.apache.http.HttpEntity;12 importorg.apache.http.HttpResponse;13 importorg.apache.http.NameValuePair;14 importorg.apache.http.client.HttpClient;15 importorg.apache.http.client.entity.UrlEncodedFormEntity;16 importorg.apache.http.client.methods.HttpGet;17 importorg.apache.http.client.methods.HttpPost;18 importorg.apache.http.impl.client.DefaultHttpClient;19 importorg.apache.http.message.BasicNameValuePair;20

21 importandroid.os.Bundle;22 importandroid.app.Activity;23 importandroid.util.Log;24 importandroid.view.Menu;25 importandroid.view.View;26 importandroid.view.View.OnClickListener;27 importandroid.widget.Button;28 importandroid.widget.EditText;29 importandroid.widget.TextView;30

31 public class HttpDemo2Activity extendsActivity32 {33 private String TAG = "http";34 private EditText mNameText = null;35 private EditText mAgeText = null;36

37 private Button getButton = null;38 private Button postButton = null;39

40 private TextView mResult = null;41

42 //基本地址:服务器ip地址:端口号/Web项目逻辑地址+目标页面(Servlet)的url-pattern

43 private String baseURL = "http://192.168.11.6:8080/HelloWeb/servlet/WelcomeUserServlet";44

45 @Override46 protected voidonCreate(Bundle savedInstanceState)47 {48 Log.i(TAG, "onCreate");49 super.onCreate(savedInstanceState);50 setContentView(R.layout.activity_http_demo2);51

52 mNameText =(EditText) findViewById(R.id.name);53 mAgeText =(EditText) findViewById(R.id.age);54 mResult =(TextView) findViewById(R.id.result);55

56 getButton =(Button) findViewById(R.id.submit_get);57 getButton.setOnClickListener(mGetClickListener);58 postButton =(Button) findViewById(R.id.submit_post);59 postButton.setOnClickListener(mPostClickListener);60 }61

62 private OnClickListener mGetClickListener = newView.OnClickListener()63 {64

65 @Override66 public voidonClick(View v)67 {68 Log.i(TAG, "GET request");69 //先获取用户名和年龄

70 String name =mNameText.getText().toString();71 String age =mAgeText.getText().toString();72

73 //使用GET方法发送请求,需要把参数加在URL后面,用?连接,参数之间用&分隔

74 String url = baseURL + "?username=" + name + "&age=" +age;75

76 //生成请求对象

77 HttpGet httpGet = newHttpGet(url);78 HttpClient httpClient = newDefaultHttpClient();79

80 //发送请求

81 try

82 {83

84 HttpResponse response =httpClient.execute(httpGet);85

86 //显示响应

87 showResponseResult(response);//一个私有方法,将响应结果显示出来

88

89 }90 catch(Exception e)91 {92 e.printStackTrace();93 }94

95 }96 };97

98 private OnClickListener mPostClickListener = newView.OnClickListener()99 {100

101 @Override102 public voidonClick(View v)103 {104 Log.i(TAG, "POST request");105 //先获取用户名和年龄

106 String name =mNameText.getText().toString();107 String age =mAgeText.getText().toString();108

109 NameValuePair pair1 = new BasicNameValuePair("username", name);110 NameValuePair pair2 = new BasicNameValuePair("age", age);111

112 List pairList = new ArrayList();113 pairList.add(pair1);114 pairList.add(pair2);115

116 try

117 {118 HttpEntity requestHttpEntity = newUrlEncodedFormEntity(119 pairList);120 //URL使用基本URL即可,其中不需要加参数

121 HttpPost httpPost = newHttpPost(baseURL);122 //将请求体内容加入请求中

123 httpPost.setEntity(requestHttpEntity);124 //需要客户端对象来发送请求

125 HttpClient httpClient = newDefaultHttpClient();126 //发送请求

127 HttpResponse response =httpClient.execute(httpPost);128 //显示响应

129 showResponseResult(response);130 }131 catch(Exception e)132 {133 e.printStackTrace();134 }135

136 }137 };138

139 /**

140 * 显示响应结果到命令行和TextView141 *@paramresponse142 */

143 private voidshowResponseResult(HttpResponse response)144 {145 if (null ==response)146 {147 return;148 }149

150 HttpEntity httpEntity =response.getEntity();151 try

152 {153 InputStream inputStream =httpEntity.getContent();154 BufferedReader reader = new BufferedReader(newInputStreamReader(155 inputStream));156 String result = "";157 String line = "";158 while (null != (line =reader.readLine()))159 {160 result +=line;161

162 }163

164 System.out.println(result);165 mResult.setText("Response Content from server: " +result);166 }167 catch(Exception e)168 {169 e.printStackTrace();170 }171

172 }173

174 }

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐