如何通过Android向特定URL高效发送GET和POST请求?
- 内容介绍
- 文章标签
- 相关推荐
本文共计661个文字,预计阅读时间需要3分钟。
javaAndroid中使用URL发送GET和POST请求,示例代码如下:
javapackage cn.com.sino_device.msmartdemo.util;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;
public class HttpUtil { public static void main(String[] args) { String urlString=指定URL; String getResponse=sendGetRequest(urlString); String postResponse=sendPostRequest(urlString, 参数); System.out.println(GET请求结果: + getResponse); System.out.println(POST请求结果: + postResponse); }
public static String sendGetRequest(String urlString) { try { URL url=new URL(urlString); BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; StringBuffer response=new StringBuffer(); while ((inputLine=in.readLine()) !=null) { response.append(inputLine); } in.close(); return response.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }
public static String sendPostRequest(String urlString, String params) { try { URL url=new URL(urlString); PrintWriter out=new PrintWriter(url.openConnection().getOutputStream()); out.print(params); out.close();
BufferedReader in=new BufferedReader(new InputStreamReader(url.openConnection().getInputStream())); String inputLine; StringBuffer response=new StringBuffer(); while ((inputLine=in.readLine()) !=null) { response.append(inputLine); } in.close(); return response.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }}
Android向指定URL发送GET和POST请求package cn.com.sino_device.msmartdemo.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* Android向指定URL发送GET和POST请求
*/
public class HttpRequest {
/**
* 向指定URL发送GET请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map
本文共计661个文字,预计阅读时间需要3分钟。
javaAndroid中使用URL发送GET和POST请求,示例代码如下:
javapackage cn.com.sino_device.msmartdemo.util;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;
public class HttpUtil { public static void main(String[] args) { String urlString=指定URL; String getResponse=sendGetRequest(urlString); String postResponse=sendPostRequest(urlString, 参数); System.out.println(GET请求结果: + getResponse); System.out.println(POST请求结果: + postResponse); }
public static String sendGetRequest(String urlString) { try { URL url=new URL(urlString); BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; StringBuffer response=new StringBuffer(); while ((inputLine=in.readLine()) !=null) { response.append(inputLine); } in.close(); return response.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }
public static String sendPostRequest(String urlString, String params) { try { URL url=new URL(urlString); PrintWriter out=new PrintWriter(url.openConnection().getOutputStream()); out.print(params); out.close();
BufferedReader in=new BufferedReader(new InputStreamReader(url.openConnection().getInputStream())); String inputLine; StringBuffer response=new StringBuffer(); while ((inputLine=in.readLine()) !=null) { response.append(inputLine); } in.close(); return response.toString(); } catch (IOException e) { e.printStackTrace(); return null; } }}
Android向指定URL发送GET和POST请求package cn.com.sino_device.msmartdemo.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* Android向指定URL发送GET和POST请求
*/
public class HttpRequest {
/**
* 向指定URL发送GET请求
*
* @param url 发送请求的URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map

