之前在自己公司做的手机OS上开发,最痛苦的就是我们的系统没有现成的HTTP协议,只有使用开源库的libCurl进行封装,在此过程中很好的熟悉了HTTP请求的一些细节,现在想想还是不错的一个经历,现在转到Android上了,对于Google来说,如果联网都处理不好的话,号称最好的互联网公司就太逊了吧。
使用URLConnection示例
下载html文件,放到文本控件中显示
public class AndroidNetActivity extends Activity {
Handler handler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText eText = (EditText)findViewById(R.id.address);
final TextView tView = (TextView)findViewById(R.id.pagetext);
handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what)
{
case 0:
{
tView.append((String)msg.obj);
// tView.setText((String)msg.obj);
break;
}
}
super.handleMessage(msg);
}
};
final Button button = (Button)findViewById(R.id.ButtonGo);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
tView.setText("");
URL url = new URL(eText.getText().toString());
URLConnection conn = url.openConnection();
// //方式1
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = "";
while((line = rd.readLine())!= null)
{
Message lmsg;
lmsg = new Message();
lmsg.what = 0;
lmsg.obj = line;
AndroidNetActivity.this.handler.sendMessage(lmsg);
}
//方式2
// InputStream is = null;
// is = conn.getInputStream();
// byte[] result=null;
//
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// while(true)
// {
// byte[] bytes = new byte[64];
// int tmp = is.read(bytes);
// if(tmp == -1)
// {
// break;
// }
// else
// {
// bos.write(bytes,0,tmp);
// }
// }
// result = bos.toByteArray();
// String all;
// all = new String(result);
//
// Message msg;
// msg = new Message();
// msg.what = 0;
// msg.obj = all;
// AndroidNetActivity.this.handler.sendMessage(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
Handler handler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText eText = (EditText)findViewById(R.id.address);
final TextView tView = (TextView)findViewById(R.id.pagetext);
handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what)
{
case 0:
{
tView.append((String)msg.obj);
// tView.setText((String)msg.obj);
break;
}
}
super.handleMessage(msg);
}
};
final Button button = (Button)findViewById(R.id.ButtonGo);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
tView.setText("");
URL url = new URL(eText.getText().toString());
URLConnection conn = url.openConnection();
// //方式1
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = "";
while((line = rd.readLine())!= null)
{
Message lmsg;
lmsg = new Message();
lmsg.what = 0;
lmsg.obj = line;
AndroidNetActivity.this.handler.sendMessage(lmsg);
}
//方式2
// InputStream is = null;
// is = conn.getInputStream();
// byte[] result=null;
//
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// while(true)
// {
// byte[] bytes = new byte[64];
// int tmp = is.read(bytes);
// if(tmp == -1)
// {
// break;
// }
// else
// {
// bos.write(bytes,0,tmp);
// }
// }
// result = bos.toByteArray();
// String all;
// all = new String(result);
//
// Message msg;
// msg = new Message();
// msg.what = 0;
// msg.obj = all;
// AndroidNetActivity.this.handler.sendMessage(msg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
HttpURLConnection
继承自URLConnection ,相当于封装的HTTP协议吧。示例代码
HttpURLConnection conn=null;
URL url;
conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/octet-stream, */*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","application/octet-stream");
conn.connect();
int code=conn.getResponseCode();
int len=conn.getContentLength();
if(conn==null)
{
return null;
}
InputStream is=null;
byte[] result=null;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
is=conn.getInputStream();
while(true){
byte[] bytes = new byte[64];
int tmp=is.read(bytes);
if(tmp==-1)
{
break;
}
else
{
bos.write(bytes, 0, tmp);
}
}
result = bos.toByteArray();
URL url;
conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/octet-stream, */*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","application/octet-stream");
conn.connect();
int code=conn.getResponseCode();
int len=conn.getContentLength();
if(conn==null)
{
return null;
}
InputStream is=null;
byte[] result=null;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
is=conn.getInputStream();
while(true){
byte[] bytes = new byte[64];
int tmp=is.read(bytes);
if(tmp==-1)
{
break;
}
else
{
bos.write(bytes, 0, tmp);
}
}
result = bos.toByteArray();
下次整理一下HTTPClient的