http get post 请求

1       建立Http连接的步骤:

1.1      获得一个http的连接地址(如:String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";)

1.2      构造一个URL对象(如:url = new URL(httpurl);)

1.3      使用HttpURLConnection打开一个连接(如:HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();)

1.4      得到读取的内容(流)(如:InputStream inputStream = httpConnection.getInputStream();)

2     TestHttpGetPostActivity:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestHttpGetPostActivity extends Activity
{
private Button mButton01;
private Button mButton02;
private Button mButton03;
private String data = null;
private TextView mTextView;
private Intent intent = new Intent();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton01 = (Button) findViewById(R.id.get);
mButton02 = (Button) findViewById(R.id.post);
mButton03 = (Button) findViewById(R.id.nothing);
mTextView = (TextView) findViewById(R.id.textview);
mButton01.setOnClickListener(listener);
mButton02.setOnClickListener(listener);
mButton03.setOnClickListener(listener);
}

private OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
// get请求
case R.id.get:
try
{
String httpurl = "http://192.168.0.68:8090/Test/index.jsp?par=this-is-get-Method-request!";
URL url;
url = new URL(httpurl);
if (url != null)
{
// 使用HttpURLConnection打开网络连接
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while (bufferedReader.readLine() != null)
{
data += bufferedReader.readLine() + "\n";
}
// 关闭inputStream
inputStream.close();
// 关闭http连接
httpConnection.disconnect();
System.out.println("data = " + data);
if (data != null)
{
intent.putExtra("data", data);
intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
startActivity(intent);
}
else
{
mTextView.setText("data is NULL!");
}
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
mTextView.setText("url is NULL!");
e.printStackTrace();
}
break;

// post请求
case R.id.post:
try
{
String httpurl = "http://192.168.0.68:8090/Test/index.jsp";
URL url;
url = new URL(httpurl);
if (url != null)
{
// 使用HttpURLConnection打开网络连接
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
// 因为是post请求,需要设置成true
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
// 设置post请求方式
httpConnection.setRequestMethod("POST");
// post请求不能使用缓存
httpConnection.setUseCaches(false);
httpConnection.setInstanceFollowRedirects(true);

// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成
// 要注意的是connection.getOutputSream会隐含地进行connect.urlConn.connect();
// DataOutputSream流
DataOutputStream outputStream = new DataOutputStream(httpConnection.getOutputStream());
// 要上传的参数
String content = "par=" + URLEncoder.encode("this is post request!", "gb2312");
// 将要上传的内容写入流中
outputStream.writeBytes(content);
// 刷新,关闭
outputStream.flush();
outputStream.close();
InputStream inputStream = httpConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while (bufferedReader.readLine() != null)
{
data += bufferedReader.readLine() + "\n";
}
// 关闭inputStream
inputStream.close();
// 关闭http连接
httpConnection.disconnect();
System.out.println("data = " + data);
if (data != null)
{
intent.putExtra("data", data);
intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
startActivity(intent);
}
else
{
mTextView.setText("data is NULL!");
}
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
mTextView.setText("url is NULL!");
e.printStackTrace();
}
break;
case R.id.nothing:
try
{
String httpurl = "http://192.168.0.68:8090/Test/index.jsp";
URL url;
url = new URL(httpurl);
if (url != null)
{
// 使用HttpURLConnection打开网络连接
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while (bufferedReader.readLine() != null)
{
data += bufferedReader.readLine() + "\n";
}
// 关闭inputStream
inputStream.close();
// 关闭http连接
httpConnection.disconnect();
System.out.println("data = " + data);
if (data != null)
{
intent.putExtra("data", data);
intent.setClass(TestHttpGetPostActivity.this, ShowHtmlContent.class);
startActivity(intent);
}
else
{
mTextView.setText("data is NULL!");
}
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
mTextView.setText("url is NULL!");
e.printStackTrace();
}
break;
}
}
};
}

 

3       ShowHtmlContent:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ShowHtmlContent extends Activity
{
private TextView mTextView;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
mTextView = (TextView) findViewById(R.id.textview);
Intent intent = ShowHtmlContent.this.getIntent();
String dataString = intent.getStringExtra("data");
if (dataString != null)
{
mTextView.setText(dataString);
}
else
{
mTextView.setText("nothing to show!");
}
}
}

4    效果图

     




posted @ 2012-01-03 16:14  程序学习笔记  阅读(543)  评论(0编辑  收藏  举报