<pre name="code" class="java"><span style="font-size:18px;"><strong>通过POST方式请求URL并传入参数并获取返回值</strong></span>
/**
* 调用接口
* @param wsurl
* @param Parameters
* @return
*/
public static String RequestUrl(String wsurl,String Parameters)
{
String rvalue = "";
try {
//声明URL
URL url = new URL(wsurl);
//打开连接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置连接方式
conn.setRequestMethod("POST");
//设置是否输入参数
conn.setDoOutput(true);
//输入参数
conn.getOutputStream().write(Parameters.getBytes());
//获取返回值
InputStream inStream = conn.getInputStream();
//流转化为字符串
rvalue = StreamTools.streamToStr(inStream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rvalue;
}
通过GET方式请求URL并获取返回值
HttpURLConnection connection = null;
Reader read;
BufferedReader bufferReader;
try {
URL url = new URL(weatherurl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(8000);
connection.setRequestMethod("GET");
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
获取读取的方式
read=new InputStreamReader(connection.getInputStream());
bufferReader=new BufferedReader(read);
//获取服务器返回的字符串
String str;//读取每一行数据
StringBuffer buffer=new StringBuffer();//接受全部数据
while((str=bufferReader.readLine())!=null){
buffer.append(str + "\n");
}
//关闭连接
read.close();
connection.disconnect();
//测试
Log.d("发出去的请求",weatherurl.toString());
Log.d("读取来的数据",buffer.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
HttpClient请求URL并获取返回值
<pre name="code" class="java">HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(weatherurl);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String jsonString = EntityUtils.toString(
httpResponse.getEntity(), "utf-8");
}
package com.example.pagedemo.utils;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StreamTools {
/**
* 把流对象转换成字符串对象
*
* @param is
* @return
*/
public static String streamToStr(InputStream is) {
try {
// 定义字节数组输出流对象
ByteArrayOutputStream os = new ByteArrayOutputStream();
// 定义读取的长度
int len = 0;
// 定义读取的缓冲区
byte buffer[] = new byte[1024];
// 按照定义的缓冲区进行循环读取,直到读取完毕为止
while ((len = is.read(buffer)) != -1) {
// 根据读取的长度写入到字节数组输出流对象中
os.write(buffer, 0, len);
}
// 关闭流
is.close();
os.close();
// 把读取的字节数组输出流对象转换成字节数组
byte data[] = os.toByteArray();
// 按照指定的编码进行转换成字符串(此编码要与服务端的编码一致就不会出现乱码问题了,android默认的编码为UTF-8)
return new String(data, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static String streamToString(InputStream is)
{
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
sb.append(line);
}
return sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}