三套API实现发送GET、POST请求给服务器

mac2022-06-30  136

1.     使用URL类实现发请求给服务器:

1.1.  Get方式的请求:

//注意: 此处 get 方式的请求, 如果带过去的请求参数 有 中文字符, 那么需要进行 //url 编码 , 否则是带不过去的 . //number=球球 number= URLEncoder.encode(number, "UTF-8"); path = path+"?number="+number+"&password="+password; URL url = new URL(path);

 

get方式时:服务端要解决乱码, 需要手动的进行 解码

String encodeValue = URLEncoder.encode(number, "ISO8859-1");

number = URLDecoder.decode(encodeValue, "UTF-8");

1.2.  Post方式的请求:

URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); //使用 post 方式 conn.setRequestMethod("POST"); //number=小銮&password=sdfds //post请求时, 中文数据也需要进行 URL 编码 //这里需要 将 number的中文值, 进行 url 编码 number = URLEncoder.encode(number, "UTF-8"); System.out.println(number); String params = "number="+number+"&password="+password; //设置必要的请求的头的信息 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", params.length()+""); //将参数 params 以流的形式 写给 服务器 //加一个 标志, 表示 要向 服务器 写数据了 conn.setDoOutput(true); conn.getOutputStream().write(params.getBytes()); int code = conn.getResponseCode();

 

Post方式 ,只需要一行代码搞定

//post是 解决乱码 // 这个代码只对post 有效, 因为覆盖的 请求体中的数据的 解码 使用到的编码集

      request.setCharacterEncoding("utf-8");

 

2.     Apache的面向对象的api实现发送请求:

2.1:   get方式:

这套 api , 谷歌工程师已经集成 到 android 中去了. 所以可以直接拿过来用

// http://188.188.4.100:8080/day06_android/login?number=110&password=123 number =URLEncoder.encode(number, "UTF-8"); path = path+"?number="+number+"&password="+password; //相当于打开了一个 浏览器客户端 HttpClient client = new DefaultHttpClient(); //get方式的请求 HttpGet get = new HttpGet(path); //获得的响应 对象 HttpResponse response = client.execute(get); //获得状态行对象,然后再 获得状态行中的状态码 int code = response.getStatusLine().getStatusCode(); if(code==200){ //获得响应体, 获得响应体中的 流的数据 //接下来的代码跟之前一样的 InputStream in = response.getEntity().getContent(); String value = StreamTool.decodeStream(in);

 

2.2: post方式

HttpClient client = new DefaultHttpClient(); // http://188.188.4.100:8080/day06_android/login HttpPost post = new HttpPost(path); //两个键值对 NameValuePair pair1=new BasicNameValuePair("number", number); NameValuePair pair2=new BasicNameValuePair("password", password); //把两个键值对 放到 list 中去, 然后再 把 list 放入到 实体中 List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(pair1); list.add(pair2); //这里传 了 UTF-8 就可以将数据 编码后带过去 了 post.setEntity(new UrlEncodedFormEntity(list,"UTF-8")); HttpResponse response = client.execute(post); //获得状态行对象,然后再 获得状态行中的状态码 int code = response.getStatusLine().getStatusCode();

 

3     使用开源框架使用发送请求:

去github上下载搜索async http ,下载把com文件放到工程目录下就可以用了,它使用的是AsyncHttpClient这个api

3.1:   get方式:

path = path+"?number="+number+"&password="+password; AsyncHttpClient client = new AsyncHttpClient(); client.get(path, new AsyncHttpResponseHandler(){

//访问 网络时, 服务器成功的处理了客户端的请求时 会被调用 @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, "登录的结果是 : " + new String(responseBody), 0).show(); }

//访问 网络时, 请求失败会调用 @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(MainActivity.this, "对不起, 服务器无响应 ... ", 0).show(); } });

3.2: post方式

AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.add("number", number); params.add("password", password); client.post(path, params,new AsyncHttpResponseHandler(){

//访问 网络时, 服务器成功的处理了客户端的请求时 会被调用 @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, "post 登录的结果是 : " + new String(responseBody), 0).show(); }

//访问 网络时, 请求失败会调用 @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(MainActivity.this, "对不起, 服务器无响应 ... ", 0).show(); } });

 

转载于:https://www.cnblogs.com/MarsDabiaoge/p/5110100.html

最新回复(0)