标签:llb inpu weight ons mes onclick == return perm
@OnClick(R.id.btn) public void onViewClicked() { final String url = mEt.getText().toString().trim(); if (TextUtils.isEmpty(url)) { Toast.makeText(this, "网址不能为空" + url, Toast.LENGTH_SHORT).show(); } new Thread(new Runnable() { @Override public void run() { try { URL Url = new URL(url); HttpURLConnection conn = (HttpURLConnection) Url.openConnection(); int code = conn.getResponseCode(); Log.d(TAG, "run: " + code); if (code == 200) { InputStream is = conn.getInputStream(); Log.d(TAG, "run: " + is); String name = StreamUtils.StreamToString(is); Log.d(TAG, "run: " + name); Message message = mHandler.obtainMessage(); message.obj = name; mHandler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
public class StreamUtils { private static ByteArrayOutputStream mBaos; public static String StreamToString(InputStream is) { try { mBaos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { mBaos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } return mBaos.toString(); } }
<uses-permission android:name="android.permission.INTERNET" />
@SuppressLint("HandlerLeak") private Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { Log.d(TAG, "handleMessage: callback"); return false; } }) { @Override public void handleMessage(Message msg) { Log.d(TAG, "handleMessage: handlemessage"); String name = (String) msg.obj; mTv.setText(name); super.handleMessage(msg); } };
Message message = mHandler.obtainMessage(); message.obj = name; mHandler.sendMessage(message);
<ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> </ScrollView>
标签:llb inpu weight ons mes onclick == return perm
原文地址:https://www.cnblogs.com/nangongyibin/p/10219206.html