android php get post,Android PHP服务网络交互POST

public String post(String url, HashMap param, List files, String fileName) {

String result = "";

String end = "\r\n";

String twoHyphens = "--";

String boundary = "ABCDEFG";

try {

URL requestUrl = new URL(url);

HttpURLConnection httpURLConnection = (HttpURLConnection) requestUrl.openConnection();

httpURLConnection.setChunkedStreamingMode(128 * 1024);//128k防止内存过高

//允许输入输出流

httpURLConnection.setDoInput(true);

httpURLConnection.setDoOutput(true);

httpURLConnection.setUseCaches(false);

httpURLConnection.setRequestMethod("POST");

httpURLConnection.setRequestProperty("Connection", "Keep-Alive");

httpURLConnection.setRequestProperty("Charset", "UTF-8");

httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());

//字段

Iterator iterator = param.entrySet().iterator();

while (iterator.hasNext()) {

dos.writeBytes(twoHyphens + boundary + end);

Map.Entry entry = (Map.Entry) iterator.next();

dos.writeBytes("Content-Disposition:form-data;name=\"" + entry.getKey() + "\"" + end);

dos.writeBytes(end);

dos.writeBytes(URLEncoder.encode(entry.getValue().toString(),"UTF-8"));

dos.writeBytes(end);

}

//文件

for (File file : files) {

dos.writeBytes(twoHyphens + boundary + end);

dos.writeBytes("Content-Disposition:form-data;name=\"" + fileName + "\";filename=\"" + file.getName() + "\"" + end);

dos.writeBytes(end);

FileInputStream fis = new FileInputStream(file);

byte[] buffer = new byte[8 * 1024];

int count = 0;

while ((count = fis.read(buffer)) != -1) {

dos.write(buffer, 0, count);

}

fis.close();

dos.writeBytes(end);

}

dos.writeBytes(twoHyphens + boundary + twoHyphens + end);

dos.flush();

InputStream is = httpURLConnection.getInputStream();

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

BufferedReader br = new BufferedReader(isr);

result = br.readLine();

dos.close();

is.close();

} catch (Exception e) {

e.printStackTrace();

Log.e("Android", e.getLocalizedMessage());

}

return result;

}

public String post(String url, HashMap param, List files, String fileName) {}

url:请求地址

param:请求参数

files:要传送的文件

fileName:接受文件要用的名字

自己撸的服务端是PHP的

感觉自己撸的传输速度还是比较快的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值