3
回答
下一行的log输出了,上一行的settext为什么没显示出效果来。
华为云实践训练营,热门技术免费实践!>>>   

关键代码是这样的:

Handler gethandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			output += "\n开始获取联网数据\n";
			textView.setText(output);
			Log.e("input", "开始获取联网数据\n");
			getinputstream();
		};
	};
联网获取数据是用的这种方式:

inputStream = url.openStream();
结果能在logcat中看到log信息,但是textview中显示不出效果来。

下面附上全部代码,也希望有人能对我的程序提些意见,小弟初学联网这一块,感觉好难,不知不觉程序就崩溃了,还是不报错的那种。

package com.example.trysomething;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

@SuppressLint("DefaultLocale")
public class MainActivity extends Activity {

	TextView textView;

	InputStream inputStream;
	String output;

	@SuppressLint("DefaultLocale")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		textView = (TextView) this.findViewById(R.id.t);

		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		
		if (networkInfo == null) {
			textView.setText("当前无法联网");
		}else {
			if (networkInfo.isAvailable()&&networkInfo.isConnected()) {
				if (networkInfo.getExtraInfo() == null) {
					textView.setText("wifi");
				}else {
					textView.setText(networkInfo.getExtraInfo().toLowerCase());
				}
				output = textView.getText().toString();
				new Thread(){
					public void run() {
						try {
							sleep(2000);
							gethandler.sendMessage(gethandler.obtainMessage());
							sleep(2000);
							outputhandler.sendMessage(outputhandler.obtainMessage());
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					};
				}.start();
			}else {
			textView.setText("当前无法连接");
			}
		}
		
}
	Handler gethandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			output += "\n开始获取联网数据\n";
			textView.setText(output);
			Log.e("input", "开始获取联网数据\n");
			getinputstream();
		};
	};
	Handler outputhandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			output += "开始输出数据\n";
			textView.setText(output);
			Log.e("input", "开始输出数据\n");
			outputStream(inputStream);
		};
	};


	public void getinputstream() {
		String string = "http://ftp121455.host181.web522.com/xiaoshanshi/daoyu.txt";
		output += "\n";
		try {
			URL url = new URL(string);
			try {
				
				//
				inputStream = url.openStream();
				
				
				
				Log.e("input", "联网获取数据成功\n");
				output += "联网获取数据成功\n";
				textView.setText(output);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("input", "联网获取数据失败\n");
				output += "联网获取数据失败\n";
				textView.setText(output);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			output += "url failed\n";
		}
		textView.setText(output);
		
	}
	public void outputStream(InputStream inputStream) {
		InputStreamReader inputStreamReader;
		BufferedReader bufferedReader;
		StringBuffer stringBuffer = new StringBuffer();

		

		
		if (inputStream != null) {
			Log.e("input", "inputStream != null\n");
			
			inputStreamReader = new InputStreamReader(inputStream);
			bufferedReader = new BufferedReader(inputStreamReader);
			String line = new String();
			
			
			try {
				while ((line = bufferedReader.readLine()) != null) {
					stringBuffer.append(line);
					stringBuffer.append("\n");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			output += stringBuffer;
			textView.setText(output);
		}else {
			
			Log.e("input", "数据为空\n");
			output += "数据为空\n";
			textView.setText(output);
			
		}
	}
}

举报
乔安同
发帖于4年前 3回/518阅
顶部