程序源代码。。
package com.ComDong.lunchmenutablueinkisb;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;public class MainActivity extends Activity {
private TextView tvshowget;
private Button btnget;
private Handler handler;
private String HtmlSourceCode="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvshowget = (TextView) findViewById(R.id.tvshowget);
btnget=(Button) findViewById(R.id.btnget);
handler=new Handler(){
public void handleMessage(Message msg){
tvshowget.setText(HtmlSourceCode);
super.handleMessage(msg);
}
};
btnget.setOnClickListener(new OnClickListener(){
public void onClick(View v){
new Thread(new Runnable(){
public void run(){
getHtmlSourceCodeM2("http://www.baidu.com/");
Message msg=handler.obtainMessage();
handler.handleMessage(msg);
}
}).start();
/* for(int i=0;i<5;i++){
if(HtmlSourceCode!="")
tvshowget.setText(HtmlSourceCode);
else
handler.postDelayed(this,1000);*/
}
});
}
public void getHtmlSourceCode(String path) throws Exception{
URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream in = conn.getInputStream();
byte[] data = readInputStream(in);
String html = new String(data);
tvshowget.setText(html);
}
public byte[] readInputStream(InputStream in) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
in.close();
return outStream.toByteArray();
}
public void getHtmlSourceCodeM2(String path){
HttpClient httpclient=new DefaultHttpClient();
HttpGet httpRequest=new HttpGet(path);
HttpResponse httpResponse;
try{
httpResponse=httpclient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HtmlSourceCode=EntityUtils.toString(httpResponse.getEntity());
}else{
Toast.makeText(MainActivity.this, "Failed to connect to the Internet", 1);
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
}
xml代码
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Get" />
<TextView
android:id="@+id/tv_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/btn_get"
android:text="Default" /></RelativeLayout>permission有Internet错误信息:
08-17 23:02:07.583: W/dalvikvm(16996): threadid=11: thread exiting with uncaught exception (group=0x41f72930)
08-17 23:02:07.583: E/AndroidRuntime(16996): FATAL EXCEPTION: Thread-1077
08-17 23:02:07.583: E/AndroidRuntime(16996): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.checkForRelayout(TextView.java:6452)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3696)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3554)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3529)
08-17 23:02:07.583: E/AndroidRuntime(16996): at com.ComDong.lunchmenutablueinkisb.MainActivity$1.handleMessage(MainActivity.java:35)
08-17 23:02:07.583: E/AndroidRuntime(16996): at com.ComDong.lunchmenutablueinkisb.MainActivity$2$1.run(MainActivity.java:46)
08-17 23:02:07.583: E/AndroidRuntime(16996): at java.lang.Thread.run(Thread.java:856)
08-17 23:02:07.653: D/dalvikvm(16996): GC_CONCURRENT freed 153K, 9% free 3840K/4184K, paused 2ms+1ms, total 18ms谢谢多线程AndroidUI
package com.ComDong.lunchmenutablueinkisb;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;public class MainActivity extends Activity {
private TextView tvshowget;
private Button btnget;
private Handler handler;
private String HtmlSourceCode="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvshowget = (TextView) findViewById(R.id.tvshowget);
btnget=(Button) findViewById(R.id.btnget);
handler=new Handler(){
public void handleMessage(Message msg){
tvshowget.setText(HtmlSourceCode);
super.handleMessage(msg);
}
};
btnget.setOnClickListener(new OnClickListener(){
public void onClick(View v){
new Thread(new Runnable(){
public void run(){
getHtmlSourceCodeM2("http://www.baidu.com/");
Message msg=handler.obtainMessage();
handler.handleMessage(msg);
}
}).start();
/* for(int i=0;i<5;i++){
if(HtmlSourceCode!="")
tvshowget.setText(HtmlSourceCode);
else
handler.postDelayed(this,1000);*/
}
});
}
public void getHtmlSourceCode(String path) throws Exception{
URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream in = conn.getInputStream();
byte[] data = readInputStream(in);
String html = new String(data);
tvshowget.setText(html);
}
public byte[] readInputStream(InputStream in) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
in.close();
return outStream.toByteArray();
}
public void getHtmlSourceCodeM2(String path){
HttpClient httpclient=new DefaultHttpClient();
HttpGet httpRequest=new HttpGet(path);
HttpResponse httpResponse;
try{
httpResponse=httpclient.execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HtmlSourceCode=EntityUtils.toString(httpResponse.getEntity());
}else{
Toast.makeText(MainActivity.this, "Failed to connect to the Internet", 1);
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
}
xml代码
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Get" />
<TextView
android:id="@+id/tv_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/btn_get"
android:text="Default" /></RelativeLayout>permission有Internet错误信息:
08-17 23:02:07.583: W/dalvikvm(16996): threadid=11: thread exiting with uncaught exception (group=0x41f72930)
08-17 23:02:07.583: E/AndroidRuntime(16996): FATAL EXCEPTION: Thread-1077
08-17 23:02:07.583: E/AndroidRuntime(16996): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.view.View.requestLayout(View.java:15473)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.checkForRelayout(TextView.java:6452)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3696)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3554)
08-17 23:02:07.583: E/AndroidRuntime(16996): at android.widget.TextView.setText(TextView.java:3529)
08-17 23:02:07.583: E/AndroidRuntime(16996): at com.ComDong.lunchmenutablueinkisb.MainActivity$1.handleMessage(MainActivity.java:35)
08-17 23:02:07.583: E/AndroidRuntime(16996): at com.ComDong.lunchmenutablueinkisb.MainActivity$2$1.run(MainActivity.java:46)
08-17 23:02:07.583: E/AndroidRuntime(16996): at java.lang.Thread.run(Thread.java:856)
08-17 23:02:07.653: D/dalvikvm(16996): GC_CONCURRENT freed 153K, 9% free 3840K/4184K, paused 2ms+1ms, total 18ms谢谢多线程AndroidUI
解决方案 »
- 我想把一个应用的代码文件打包成jar在集成另一个应用里面怎么做?
- 请问adil提供的接口可以再封装成一个类吗
- 100分的hello android问题救助
- 求教android安装问题,我安装SDK2.0和1.5都出现这个问题了。
- 请问android sdk版本和android版本有什么关系?
- java 是解释型的语言吧?
- 在eclipse里面安装apk时,出现。。
- android-Q1:ANDROID版本和ADT版本
- 基于open street map的地图引擎,支持离线地图。
- 请问android里如何判断扫描到的蓝牙设备是不是蓝牙耳机啊?
- androidpn
- eclipse中启动avd为什么总要好几次
Message msg=handler.obtainMessage();
handler.handleMessage(msg);
这里你handleMessage?应该是sendMessage吧,另外你的xml里控件的id和你java文件中的id都对不上,真不知道你怎么跑起来的