android POST请求

android用POST方式请求网络,多的不说了   直接上代码

//请求代码
public static String Post(String string,String get,Context context)//string POST参数,get 请求的URL地址,context 联系上下文
	{
		
		try {
		String urldizhi=get; //请求地址
		URL url=new URL(urldizhi);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(50000);//超时时间
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//		conn.setRequestProperty("User-Agent", Other.getUserAgent(context));
		OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
		out.write(string);
		out.flush();
		out.close();
		
		InputStream inputStream=conn.getInputStream();
		byte[] data=StreamTool.read(inputStream);
		html = new String(data, "utf-8");
		
		} catch (Exception e) {
			System.out.println("-----"+e);
			String string2="{\"success\":-1}";
			
			return string2;
		}
		return html;
	}
还有一个类我也贴出来
package com.sutong.feihua.http;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;


public class StreamTool {
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 
<span style="white-space:pre">	</span> * @param inStream
<span style="white-space:pre">	</span> * @return
<span style="white-space:pre">	</span> * @throws Exception
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static byte[] read(InputStream inStream) throws Exception{
<span style="white-space:pre">		</span>ByteArrayOutputStream outStream = new ByteArrayOutputStream();
<span style="white-space:pre">		</span>byte[] buffer = new byte[1024];
<span style="white-space:pre">		</span>int len = 0;
<span style="white-space:pre">		</span>while( (len = inStream.read(buffer)) != -1){
<span style="white-space:pre">			</span>outStream.write(buffer, 0, len);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>inStream.close();
<span style="white-space:pre">		</span>return outStream.toByteArray();
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public FileInputStream output(String fileString)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>File file = new File(fileString);
<span style="white-space:pre">		</span>FileInputStream fileInputStream = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>fileInputStream = new FileInputStream(file);
<span style="white-space:pre">		</span>} catch (FileNotFoundException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return fileInputStream;
<span style="white-space:pre">		</span>
<span style="white-space:pre">	</span>}


}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中使用POST请求向后台传递数组可以通过以下步骤实现: 1. 创建一个JSON数组对象,将需要传递的数据添加到其中。例如,以下代码创建了一个包含两个字符串的JSON数组: ``` JSONArray jsonArray = new JSONArray(); jsonArray.put("string1"); jsonArray.put("string2"); ``` 2. 将JSON数组对象转换为字符串。例如,以下代码将上面创建的JSON数组对象转换为字符串: ``` String jsonString = jsonArray.toString(); ``` 3. 创建一个HTTP POST请求对象,并设置请求头和请求体。例如,以下代码创建了一个HTTP POST请求对象: ``` String url = "http://example.com/api"; HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("Accept", "application/json"); StringEntity entity = new StringEntity(jsonString); httpPost.setEntity(entity); ``` 在上面的代码中,设置了请求头为"Content-Type"和"Accept",并将请求体设置为上面转换的JSON数组字符串。 4. 发送HTTP POST请求并处理响应。例如,以下代码发送HTTP POST请求并处理响应: ``` HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { String responseBody = EntityUtils.toString(response.getEntity()); // 处理响应 } else { // 处理错误 } ``` 在上面的代码中,使用HttpClient发送HTTP POST请求,并检查响应的状态码。如果状态码为200(HttpStatus.SC_OK),则将响应的实体转换为字符串并进行处理。否则,处理错误。 以上是向后台传递数组的一种方法,具体实现可能会根据后台API的设计而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值