android post 服务器参数设置,android之发送Get或Post请求至服务器接口

该代码段展示了如何使用Java进行HTTP GET和POST请求,获取并处理返回的数据。主要涉及了HttpURLConnection类,包括设置代理、请求头、超时时间等,并对返回的XML数据进行了清理。
摘要由CSDN通过智能技术生成

importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importandroid.annotation.SuppressLint;importandroid.os.StrictMode;importandroid.util.Log;

@SuppressLint("NewApi")public classServiceUtil {public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";public staticString getServiceInfo (String strUrl,String ip,String port)

{

System.setProperty("https.proxyHost", ip);

System.setProperty("https.proxyPort", port);

StrictMode.setThreadPolicy(newStrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

StrictMode.setVmPolicy(newStrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

HttpURLConnection conn= null;

BufferedReader reader= null;

String rs= null;try{

StringBuffer sb= newStringBuffer();

URL url= newURL(strUrl);

conn=(HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setRequestProperty("User-agent", userAgent);

conn.setUseCaches(false);

conn.setConnectTimeout(90000);

conn.setReadTimeout(90000);

conn.setInstanceFollowRedirects(false);

conn.connect();

InputStream is=conn.getInputStream();

reader= new BufferedReader(new InputStreamReader(is, "UTF-8"));

String strRead= null;while ((strRead = reader.readLine()) != null) {

sb.append(strRead);

}

rs=sb.toString();

}catch(IOException e) {

Log.e("ServiceUtil-->>getServiceInfo",e.getMessage());

}finally{if (reader != null) {try{

reader.close();

}catch(IOException e) {

Log.e("ServiceUtil-->>getServiceInfo",e.getMessage());

}

}if (conn != null) {

conn.disconnect();

}

}if(null!=rs){

rs= rs.replace( "<?xml version=\"1.0\" encoding=\"utf-8\"?>", "")

.replace("", "")

.replace("", "").replace("\r", "")

.replace("\n", "")

.replace("", "");

}

LogUtil.WriteLog(rs);returnrs;

}/*** post请求获取数据

*@paramstrUrl 请求地址,如: "http://....asmx/HelloWork"

*@paramparams 设置发送的参数,如:"key=1&name=1";//设置发送的参数

*@return

*/

public staticString getServiceInfoPost(String strUrl,String params)

{

String strResult="";try{byte[] entity =params.getBytes();

HttpURLConnection con= (HttpURLConnection) newURL(strUrl).openConnection();

con.setRequestMethod("POST");

con.setDoOutput(true);

con.setConnectTimeout(90000);

con.setReadTimeout(90000);

con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

con.setRequestProperty("Content-Length",String.valueOf(entity.length));

con.getOutputStream().write(entity);int code =con.getResponseCode();//响应代码 200表示成功

if(code==200){

InputStream inStream=con.getInputStream();

strResult=new String(readInputStream(inStream), "UTF-8");if(null!=strResult){

strResult= strResult.replace( "", "")

.replace("", "")

.replace("", "").replace("\r", "")

.replace("\n", "")

.replace("", "");

}

}

}catch(Exception ex){

Log.e("getServiceInfoPost",ex.getMessage());

}returnstrResult;

}/*** 从输入流中读取数据

*@paraminStream

*@return*@throwsException*/

public static byte[] readInputStream(InputStream inStream) {try{

ByteArrayOutputStream outStream= newByteArrayOutputStream();byte[] buffer = new byte[10000];int len = 0;while( (len = inStream.read(buffer)) !=-1){

outStream.write(buffer,0, len);

}byte[] data = outStream.toByteArray();//网页的二进制数据

outStream.close();

inStream.close();returndata;

}catch(Exception ex){return null;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值