Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 獲取網絡返回的字符串數據

android 獲取網絡返回的字符串數據

編輯:關於Android編程

public static String getString(String urlpath) throws Exception {

URL url = new URL(urlpath);

try {

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(6 * 1000);

if (conn.getResponseCode() == 200) {

InputStream inStream = conn.getInputStream();

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len = -1;

while ((len = inStream.read(buffer)) != -1) {

outStream.write(buffer, 0, len);

}

outStream.close();

inStream.close();

byte[] data = outStream.toByteArray();

return new String(data, "UTF-8");

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved