当前位置: 移动技术网 > IT编程>移动开发>Android > Android客户端post请求服务器端实例

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

吉他手法,facebook上不了,江门城轨

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则没有此限制。

注意:考虑到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;

  }

}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网