软件编程
位置:首页>> 软件编程>> Android编程>> Android客户端post请求服务器端实例

Android客户端post请求服务器端实例

作者:junjie  发布时间:2022-10-10 23:49:43 

标签:Android,客户端,post,请求,服务器端

Android客户端请求服务器端的详细解释

1. Android客户端与服务器端通信方式:
Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式。
2. 解析服务器端返回数据的解释:
(1).对于服务器端来说,返回给客户端的数据格式一般分为html、xml和json这三种格式。
(2). JSON(Javascript Object Notation)是一种轻量级的数据交换格式,相比于xml这种数据交换格式来说,因为解析xml比较的复杂,而且需要编写大段的代码,所以客户端和服务器的数据交换格式往往通过JSON来进行交换。
3. Android中,用GET和POST访问http资源
(1).客户端向服务器端发送请求的时候,向服务器端传送了一个数据块,也就是请求信息。
(2). GET和POST区别:
A: GET请求请提交的数据放置在HTTP请求协议头(也就是url)中,而POST提交的数据则放在实体数据中,安全性比较高。
B: GET方式提交的数据最多只能有1024字节,而POST则没有此限制。

Android客户端post请求服务器端实例

注意:考虑到POST的优势,在Android开发中自己认为最好用POST的请求方式,所以下面自己写了一个小的POST请求的例子。代码如下:


package com.scd.jsondemo.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil {
 /** 地址 */
 private static final String INNER_URL = "http://localhost:8080/index2.jsp";
 /** TAG */
 private final String TAG = getClass().getSimpleName();
 private static final int USER_ID = 1;

/***
  * 客户端调用的方法:传递参数向服务器中发送请求
  *
  * @param userId
  * @param userName
  * @return
  */
 public static JSONObject getData(String userId, String userName) {
   int modelId = USER_ID;
   List<NameValuePair> list = new ArrayList<NameValuePair>();
   list.add(new BasicNameValuePair("userId", userId));
   list.add(new BasicNameValuePair("userName", userName));

return doPost(modelId, list);
 }

/**
  * 请求服务器的方法
  *
  * @param model
  * @param paramList
  * @return
  */
 private static JSONObject doPost(int model, List<NameValuePair> paramList) {

// 1.创建请求对象
   HttpPost httpPost = new HttpPost(INNER_URL);
   // post请求方式数据放在实体类中
   HttpEntity entity = null;
   try {
     entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
     httpPost.setEntity(entity);
   } catch (UnsupportedEncodingException e1) {
     e1.printStackTrace();
   }

// 2.创建客户端对象
   HttpClient httpClient = new DefaultHttpClient();
   // 3.客户端带着请求对象请求服务器端
   try {
     // 服务器端返回请求的数据
     HttpResponse httpResponse = httpClient.execute(httpPost);
     // 解析请求返回的数据
     if (httpResponse != null
         && httpResponse.getStatusLine().getStatusCode() == 200) {
       String element = EntityUtils.toString(httpResponse.getEntity(),
           HTTP.UTF_8);
       if (element.startsWith("{")) {
         try {
           return new JSONObject(element);
         } catch (JSONException e) {
           e.printStackTrace();
         }
       }
     }

} catch (ClientProtocolException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }

return null;

}

}
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com