Android通过http与服务器端交互

一、post方式
        String uriAPI = "http://192.168.2.229:8088/YichaMarket/soft/list.action";
       
        HttpPost httpRequest = new HttpPost(uriAPI); 
      
        List <NameValuePair> params = new ArrayList <NameValuePair>(); 
        params.add(new BasicNameValuePair("customName", "lunzi")); 
        try 
        { 
         
          httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
         
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
          
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          { 
          
            String strResult = EntityUtils.toString(httpResponse.getEntity()); 
            
            JSONObject json = new JSONObject(strResult);
            
            mTextView1.setText(json.getString("newName")); 
          } 
          else 
          { 
            mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString()); 

          } 


在sina第三方授权中使用post请求来根据code获取access_token:

import java.io.IOException;


import java.util.ArrayList;


import java.util.List;








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.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.protocol.HTTP;


import org.apache.http.util.EntityUtils;




import org.json.JSONObject;


import android.util.Log;


/**
 * 转换code工具类
 * 
 * @author Administrator
 * 
 */


public class AccessTokenGet {


/**

* 通过code转化AccessToken

* @param client_id
*            App Key

* @param client_secret
*            App Secret

* @param redirect_uri
*            回调页面地址

* @param code
*            code
*/


public static JSONObject getAccessToken(String code) {


String result = "";
JSONObject json = null;


/* 建立HTTPost对象 */


HttpPost httpRequest = new HttpPost("https://api.weibo.com/oauth2/access_token");


try {


/* 添加请求参数到请求对象 */


List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("client_id", ConstantS.Sina_APP_KEY)); 


params.add(new BasicNameValuePair("client_secret", ConstantS.Sina_APP_SECRET)); 


params.add(new BasicNameValuePair("grant_type", "authorization_code")); 


params.add(new BasicNameValuePair("redirect_uri", ConstantS.REDIRECT_URL)); 

params.add(new BasicNameValuePair("code", code)); 


Log.i("info", params+"");



httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));








/* 发送请求并等待响应 */


HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);



/* 若状态码为200 ok */


if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {


/* 读返回数据 */


result = EntityUtils.toString(httpResponse.getEntity());

json=new JSONObject(result);




}


} catch (ClientProtocolException e) {


e.printStackTrace();


} catch (IOException e) {


e.printStackTrace();


} catch (Exception e) {


e.printStackTrace();


}




Log.i("result", result);

return json;


}


}

二、get方式
String uriAPI = "http://192.168.2.229:8088/YichaMarket/soft/list.action?customName=lunzi"; 
        HttpGet httpRequest = new HttpGet(uriAPI); 
        try 
        { 
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          { 
            String strResult = EntityUtils.toString(httpResponse.getEntity());
           
          } 
          else 
          { 
            

          }




注意:在请求数据时候记得加线程!!!切记!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值