在本篇文章中,我们要主分析宋体请求的内容,自我感觉有个不错的建议和大家分享下
可以说现在市场上的android应用和游戏,客户端和服务端交互的要主方法是通过http请求实现的,因为便方,执行速度也快。本文要主分析通过org.apache.http.client.HttpClient和java.net.HttpURLConnection实现http请求。包含Get和Post两种方法的用调方法。HttpClient要需和HttpGet、HttpPost、HttpEntity这几个类相互配合实现和服务端交互。HttpURLConnection则要主和InputStreamReader、 BufferedReader、DataOutputStream这几个类相互配合实现和服务端的交互。上面看整完的码代实现进程。
package com.figo.helloworld;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* @author zhuzhifei 客户端和服务端通过http行进交互,回返json格式字符串
*
*/
public class HttpActivity extends Activity {
private Button btnHttpClientGet;
private Button btnHttpClientPost;
private Button btnHttpUrlConGet;
private Button btnHttpUrlConPost;
private TextView tvContent;
private String tag = "HttpActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httptest);
btnHttpClientGet = (Button) findViewById(R.id.btnHttpClientGet);
btnHttpClientPost = (Button) findViewById(R.id.btnHttpClientPost);
btnHttpUrlConGet = (Button) findViewById(R.id.btnHttpUrlConGet);
btnHttpUrlConPost = (Button) findViewById(R.id.btnHttpUrlConPost);
tvContent = (TextView) findViewById(R.id.tvContent);
// HttpClient Get请求范例
btnHttpClientGet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tvContent.setText("");
String httpUrl = "http://10.241.72.168:8083/figo/json/login.htm?userName=zhuzhifei&passWord=snda1234";
// HttpGet接连象对
HttpGet httpRequest = new HttpGet(httpUrl);
// 得取支撑通普http请求的HttpClient象对
HttpClient httpclient = new DefaultHttpClient();
// 如果要需支撑Https ssl加密的请求方法则应用以下种这HttpClient
// 请求HttpClient,得取HttpResponse
HttpResponse httpResponse;
try {
httpResponse = httpclient.execute(httpRequest);
// 请求胜利
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 得取回返的字符串
String result = EntityUtils.toString(httpResponse
.getEntity());
tvContent.setText(result);
} else {
tvContent.setText("HttpGet请求犯错");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tvContent.setText("HttpGet请求犯错:" + e.getStackTrace());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tvContent.setText("HttpGet请求犯错:" + e.getStackTrace());
}
}
});
// HttpClient Post请求范例
btnHttpClientPost.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
tvContent.setText("");
// http地址
String httpUrl = "http://10.241.72.168:8083/figo/json/login.htm";
// HttpPost接连象对
HttpPost httpRequest = new HttpPost(httpUrl);
// 应用NameValuePair来保存要传递的Post参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 加添要传递的参数
params.add(new BasicNameValuePair("userName", "zhuzhifei"));
params.add(new BasicNameValuePair("passWord", "snda1234"));
// 设置字符集
HttpEntity httpentity = new UrlEncodedFormEntity(params,
"gb2312");
// 请求httpRequest
httpRequest.setEntity(httpentity);
// 得取认默的HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 得取HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
// HttpStatus.SC_OK表现接连胜利
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 得取回返的字符串
String result = EntityUtils.toString(httpResponse
.getEntity());
tvContent.setText(result);
} else {
tvContent.setText("请求误错!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
});
// HttpURLConnection Get请求范例
btnHttpUrlConGet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// 应用HttpURLConnection开打接连
try {
tvContent.setText("");
URL url = new URL(
"http://10.241.72.168:8083/figo/json/login.htm?userName=zhuzhifei&passWord=snda1234");
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 失掉取读的内容(流)
InputStreamReader in = new InputStreamReader(urlConn
.getInputStream());
// 为输出创立BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
String result = "";
// 应用循环来取读得获的数据
while (((inputLine = buffer.readLine()) != null)) {
// 我们在每一行面后加上一个"\n"来换行
result += inputLine + "\n";
}
tvContent.setText(result);
// 闭关InputStreamReader
in.close();
// 闭关http接连
urlConn.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// HttpURLConnection Post请求范例
btnHttpUrlConPost.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tvContent.setText("");
try {
// TODO Auto-generated method stub
String httpUrl = "http://10.241.72.168:8083/figo/json/login.htm";
// 得获的数据
String result = "";
URL url = new URL(httpUrl);
// 应用HttpURLConnection开打接连
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
// 因为这个是post请求,设立要需设置为true
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
// 设置以POST方法
urlConn.setRequestMethod("POST");
// Post 请求不能应用缓存
urlConn.setUseCaches(false);
urlConn.setInstanceFollowRedirects(true);
// 配置本次接连的Content-type,配置为application/x-www-form-urlencoded的
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 接连,从postUrl.openConnection()至此的配置必须要在connect之前成完,
// 要注意的是connection.getOutputStream会隐含的行进connect。
urlConn.connect();
// DataOutputStream流
DataOutputStream out = new DataOutputStream(urlConn
.getOutputStream());
// 要上传的参数
String content = "userName=zhuzhifei&passWord=snda1234";
// 将要上传的内容写入流中
out.writeBytes(content);
// 刷新、闭关
out.flush();
out.close();
// 失掉取读的内容(流)
InputStreamReader in = new InputStreamReader(urlConn
.getInputStream());
// 为输出创立BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
// 应用循环来取读得获的数据
while (((inputLine = buffer.readLine()) != null)) {
// 我们在每一行面后加上一个"\n"来换行
result += inputLine + "\n";
}
tvContent.setText(result);
} catch (Exception e) {
}
}
});
}
}
文章结束给大家分享下程序员的一些笑话语录:
真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。