http 连接解析


json

http://www.json.org/json-zh.html


String s1 = NetUtil.getPowerOnUrl(mContext);
URL url;
HttpURLConnection urlConnection =null;
try {
url = new URL(s1); 
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(100000);
  InputStream in = new BufferedInputStream(urlConnection.getInputStream());
   readStream(in);
 }catch (MalformedURLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}  finally {
	if(urlConnection!=null){
	     urlConnection.disconnect(); 
	}
 }
 
public static String fromStream(InputStream in) throws IOException
	{
	    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
	    StringBuilder out = new StringBuilder();
	    String line;
	    while ((line = reader.readLine()) != null) {
	        out.append(line);
	    }
	    return out.toString();
	}
	
	private void readStream(InputStream in) {
 		String str = "" ;
	
		// TODO Auto-generated method stub
		try {
			str = fromStream(in); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.v(TAG, str);
	}


你可能感兴趣的:(http 连接解析)