我需要阅读textview1中的文字,但文字总是与" -shutdown" 但是在textview上显示文本" -shutdown"
或者是否还有其他方法直接从bo.toString()中读取文本并将其写入文本视图?
感谢。
我的活动课程:
package com.example.androrem;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ReadSMS extends Activity {
private static final int duration = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_sms);
doButton1();
}
private void readData(){
new Thread() {
@Override
public void run() {
String path ="http://androidremoter.altervista.org/zero/test.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(bo.toString()); //the textview display "-shutdown"
String input = text.getText().toString();//get text from textview
String com1 = "-shutdown";
if(input==com1)//always show not work
{
TextView text2 = (TextView) findViewById(R.id.textView2);
text2.setText("work");
}
else
{
TextView text2 = (TextView) findViewById(R.id.textView2);
text2.setText("not work");
}
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void doButton1()
{
Button gettext = (Button) findViewById(R.id.test1);
gettext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
readData();
}
});
}
答案 0 :(得分:2)
你不能比较那两个字符串。您正在比较对象而不是内容。这样做:
if (input.trim().equals(com1)) {
...