1.HTTP请求的方法
2.使用Get方法发送请求
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private String baseUrl = "Http://192.168.0.1:8080/serverside/name";
public void onClick(View v) {
// TODO Auto-generated method stub
String name = nameView.getText().toString();
String age = ageView.getText().toString();
String url = baseUrl + "?" + "name=" + name + "age=" + age;
//生成一个请求对象
HttpGet httpGet = new HttpGet("url");
//生成一个HTTP客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用HTTP客户端发送请求对象
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpGet); //调用客户端对象的execute方法把请求对象传进去,就相当于向服务端发送了一次请求,然后服务器端会给一个相遇HttpResponse
httpEntity = httpResponse.getEntity(); // 得到一个服务器端响应的内容
inputStream = httpEntity.getContent(); // 把内容放到一个流里面
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) != null){
result = result + line;
}
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try
{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
3.使用Post放发发送请求
private String baseUrl = "Http://192.168.0.1:8080/serverside/name";
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
public void onClick(View v) {
// TODO Auto-generated method stub
String name = nameView.getText().toString();
String age = ageView.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try{
HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(baseUrl);
HttpPost.setEntity(requestEntity);
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpPost); //调用客户端对象的execute方法把请求对象传进去,就相当于向服务端发送了一次请求,然后服务器端会给一个相遇HttpResponse
httpEntity = httpResponse.getEntity(); // 得到一个服务器端响应的内容
inputStream = httpEntity.getContent(); // 把内容放到一个流里面
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) != null){
result = result + line;
}
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try
{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e) {
e.printStackTrace();
}
};
Get和Post方法的区别
在于传递数据方式的区别
Get方法
String name = nameView.getText().toString();
String age = ageView.getText().toString();
String url = baseUrl + "?" + "name=" + name + "age=" + age;
HttpGet httpGet = new HttpGet("url");
//生成一个HTTP客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用HTTP客户端发送请求对象
InputStream inputStream = null;
Post方法
String name = nameView.getText().toString();
String age = ageView.getText().toString();
NameValuePair nameValuePair1 = new BasicNameValuePair("name", name);
NameValuePair nameValuePair2 = new BasicNameValuePair("age", age);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(nameValuePair1);
nameValuePairs.add(nameValuePair2);
try{
HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);
HttpPost httpPost = new HttpPost(baseUrl);
HttpPost.setEntity(requestEntity);
HttpClient httpClient = new DefaultHttpClient();
InputStream inputStream = null;
本文详细介绍了如何使用Get和Post方法发送HTTP请求,并对比了这两种方法传递数据的不同方式。通过具体的代码示例展示了每种方法的具体实现过程。
223

被折叠的 条评论
为什么被折叠?



