关于HTTP传输的介绍请见http://blog.csdn.net/theworldsong/article/details/9107789。
以下例子分别以POST和GET方式向网站传输数据,并返回数据将其显示。
package dfzy.EX088;
/*必需引用apache.http相关类来建立HTTP联机*/
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import dfzy.EX088.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class EX088 extends Activity
{
private Button mButton1,mButton2;
private TextView mTextView1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//声明地址
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
//建立HTTP Post联机
HttpPost httpRequest = new HttpPost(uriAPI);
//Post运行传送变量必须用NameValuePair[]储存,这是一种封装,专门用于HTTP的POST传输
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try
{
//将要附带的数据包放入,并声明为UTF_8字符格式
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//发送后,会等待返回一个HttpResponse
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
//若状态码为200
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*获取字符串*/
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("获取失败");
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php?str=I+am+Get+String";
/*建立HTTP Get联机*/
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
/*发送获取的HTTP request,并等待返回一个HttpResponse*/
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/*若状态码为200*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
//获取字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("获取失败");
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
}
代码中使用的网站可能失效,但是明白道理就行,就是返回一串字符。
另参考
http://52android.blog.51cto.com/2554429/496621
转载请注明来源,版权归原作者所有,未经同意严禁用于任何商业用途。
微博:http://weibo.com/theworldsong
邮箱:theworldsong@foxmail.com