android平台HttpGet、HttpPost请求实例
使用httpclient中的httpget()方法进行http通信的实例:
/**
*description:android httpget()
*authour:yaner·gates
*website://www.jb51.net
*/
package me.gogogoog;
import java.io.ioexception;
import org.apache.http.httpresponse;
import org.apache.http.httpstatus;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.httpclient;
import org.apache.http.client.methods.httpget;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.util.entityutils;
import android.app.activity;
import android.os.bundle;
import android.widget.textview;
public class myhttpgetactivity extends activity{
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate){
super.oncreate(savedinstancestate);
setcontentview(r.layout.result);
textview resulttext = (textview) this.findviewbyid(r.id.resulttext);
string username="username";
string password="password";
string httpurl = "http://192.168.1.90:8080/androidlogin/loginaction.do?method=login&username="+username+"&password="+password;
//创建httprequest对象
httpget httprequest = new httpget(httpurl);
try
{
//取得httpclient对象
httpclient httpclient = new defaulthttpclient();
//请求httpclient,取得httpresponse
httpresponse httpresponse = httpclient.execute(httprequest);
//请求成功
if (httpresponse.getstatusline().getstatuscode() == httpstatus.sc_ok)
{
//取得返回的字符串
string strresult = entityutils.tostring(httpresponse.getentity());
resulttext.settext(strresult);
}
else
{
resulttext.settext("请求错误!");
}
}
catch (clientprotocolexception e)
{
resulttext.settext(e.getmessage().tostring());
}
catch (ioexception e)
{
resulttext.settext(e.getmessage().tostring());
}
catch (exception e)
{
resulttext.settext(e.getmessage().tostring());
}
}
}
使用httpclient中的httppost()方法进行http通信的实例:
*description:android httppost()
*authour:yaner·gates
*website://www.jb51.net
*/
package me.gogogoog;
import java.io.ioexception;
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.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.widget.textview;
public class resultactivity extends activity{
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate){
super.oncreate(savedinstancestate);
setcontentview(r.layout.result);
textview resulttext = (textview) this.findviewbyid(r.id.resulttext);
string username="username";
string password="password";
string httpurl = "http://192.168.1.90:8080/androidlogin/loginaction.do?method=login";
//创建httprequest对象
httppost httprequest = new httppost(httpurl);
list<namevaluepair> params = new arraylist<namevaluepair>();
params.add(new basicnamevaluepair("username", username));
params.add(new basicnamevaluepair("password", password));
try{
//设置字符集
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 strresult = entityutils.tostring(httpresponse.getentity());
resulttext.settext(strresult);
}else{
resulttext.settext("请求错误!");
}
}catch (clientprotocolexception e){
resulttext.settext(e.getmessage().tostring());
} catch (ioexception e){
resulttext.settext(e.getmessage().tostring());
}catch (exception e){
resulttext.settext(e.getmessage().tostring());
}
}
}
推荐阅读
-
Android使用httpPost向服务器发送请求的方法
-
Android使用httpPost向服务器发送请求的方法
-
Android中发送Http请求(包括文件上传、servlet接收)的实例代码
-
android之HttpPost&HttpGet使用方法介绍
-
Android HTTP发送请求和接收响应的实例代码
-
Android下通过httpClient发送GET和POST请求的实例代码
-
Android中发送Http请求(包括文件上传、servlet接收)的实例代码
-
Android 网络请求框架Volley实例详解
-
android之HttpPost&HttpGet使用方法介绍
-
android平台HttpGet、HttpPost请求实例