public String networker_data(String url)
{
String result ="";
// String
URL realURL =null;
URLConnection conn =null;
BufferedReader bufReader =null;
String line ="";
try {
realURL =new URL(url);
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("url 格式错误");
}
try {
// String androidid=Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
Log.d("***","http url: "+url);
conn = realURL.openConnection();
// 设置连接参数...conn.setRequestProperty("xx", "xx");
// conn.setRequestProperty("mac", getMacAddress());
conn.setConnectTimeout(10000);// 10s timeout
// conn.setRequestProperty("accept", "*/*");
// conn.setRequestProperty("", "");
conn.connect();// 连接就把参数送出去了 get方法
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("连接错误");
}
try {
bufReader =new BufferedReader(new InputStreamReader( conn.getInputStream(),"utf-8"));
while ((line = bufReader.readLine()) !=null) {
result += line +"\n";
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("读取数据错误");
}finally {
// 释放资源
if (bufReader !=null) {
try {
bufReader.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Log.d("***","http result: "+result);
return result;
}
网友评论