欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android平台HttpGet、HttpPost请求实例

程序员文章站 2022-06-04 12:18:24
使用httpclient中的httpget()方法进行http通信的实例:复制代码 代码如下:/**   *description:android ht...

使用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()); 
        }  
    } 
}