package com.example.booktest;
import java.io.BufferedReader;
import java.net.URL;
import android.util.Log;
public class GetHttpUrlConnection {
private HttpURLConnection conn;
private InputStream is;
public String getHttpUrlData(String uri){
Log.i(“aaa”, uri);
String resultData = "";
try {
URL url = new URL(uri); //URL对象
conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(3000);
/* conn.setDoInput(true); //允许输入流,即允许下载
conn.setDoOutput(true); //允许输出流,即允许上传
conn.setUseCaches(false); //不使用缓冲
*/ conn.setRequestMethod(“GET”); //使用get请求
conn.connect();
int code = conn.getResponseCode();
// Log.i(“aaa”, String.valueOf(code));
is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bufferReader = new BufferedReader(isr);
String inputLine = "";
while((inputLine = bufferReader.readLine()) != null){
resultData += inputLine + "\n";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
conn.disconnect();
}
}
return resultData;
}
}