@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //接口数据 private String url="http://www.toutiao.com/api/pc/focus/"; private void getdata(String url){ new Thread(){ }.start(); try { URL url1 = new URL(url); HttpURLConnection connection= (HttpURLConnection) url1.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.connect(); //获取请求码 int responseCode = connection.getResponseCode(); Message obtain = Message.obtain(); if (responseCode==HttpURLConnection.HTTP_OK){ /*obtain.what=HTTP_SUCCES;*/ // obtain.what=HTTP_SUCCESS; InputStream stream = connection.getInputStream(); String data = overString(stream); obtain.obj=data; Handler.sendMessage(obtain); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private String overString(InputStream input) throws IOException { ByteArrayOutputStream by = new ByteArrayOutputStream(); int len=-1; byte[] bytes = new byte[514]; while ((len=input.read(bytes))!=-1){ by.write(bytes,0,len); } return new String(by.toByteArray()); } private Handler Handler=new Handler() { @Override public void handleMessage(Message msg) { String s = (String) msg.obj; } };
Hand解析
最新推荐文章于 2021-07-13 16:46:06 发布