Android学习第十七天----从网络上获取html代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="63dp" android:text="Button" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:ems="10" android:text="http://192.168.1.20:8080/javawebhtml/"> </EditText> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="59dp" android:text="TextView" /> </RelativeLayout>
package com.example.httphtml; import java.io.IOException; import com.will.service.HtmlService; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText edit; private Button mButton; private TextView text; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 100) { text.setText((String) msg.obj); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit = (EditText) findViewById(R.id.editText1); mButton = (Button) findViewById(R.id.button1); text = (TextView) findViewById(R.id.textView1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = 100; byte[] b; try { b = HtmlService.getText(edit.getText().toString()); msg.obj = new String(b); handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); } }
package com.will.service; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class HtmlService { public static byte[] getText(String path) throws IOException { InputStream is = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5*1000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200) { is = conn.getInputStream(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return getByte(is); } public static byte[] getByte(InputStream is) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = 0; while((len = is.read(b))!= -1) { baos.write(b, 0, len); } return baos.toByteArray(); } }