Android - 我如何在TextView中打印结果?

问题描述 投票:0回答:5

我应该使用setText(),但我应该如何使用它而不是System.out.println()?我想在TextView中打印结果。这段代码是一个json阅读器,对不起,我需要写一些文字来让我发布我的问题。

public class JsonReader extends Activity{

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

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}

public static void main(String[] args) throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("http://www.w3schools.com/json/myTutorials.txt");
    System.out.println(json.toString());
    System.out.println(json.get("display"));
    //TextView tv = (TextView) findViewById( R.id.tv );
    //tv.setText(json.toString());
    //tv.setText(json.get("display"));

}

}

java android textview
5个回答
3
投票

如果你想在手机上查看结果,而不是像johnrao07建议的那样在LogCat控制台查看结果,你需要在TextView中打印结果。

要在TextView中打印结果,首先在activity_main.xml布局文件中添加一个TextView部件。

<TextView
        android:id="@+id/text_view_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

然后用JsonReader.java类的main函数来代替。System.out.println(json.toString()); 召唤 ((TextView) findViewById(R.id.text_view_id)).setText(json.toString());


1
投票

您需要使用 日志 来打印和调试android中的东西。

一般来说,有 五法,

Log.v() Log.d() Log.i() Log.w() and Log.e()

v代表啰嗦

d为Debug

i供参考

警告的w

e为错误

你的情况是

Log.d("Key", json.toString() + "");
Log.d("Key", json.get("display") + "");

而你在日志猫中寻找值,使用 "键"


0
投票

您可以使用 CharArrayWriter 来收集输出。

CharArrayWriter writer = new CharArrayWriter();
// write your stuff
TextView view = ...
view.setText(writer.toString());

你将不得不使用可用的方法来收集输出: Writer 而不是那些 PrintStream 诸如 System.out (没有 println() 方法)。)

如果你想使用基本相同的调用来生成输出,你可以创建一个叫做 ByteArrayOutputStream 并将其包裹在 PrintStream. 写完后(并关闭 PrintStream),从该文件中检索字节数组。ByteArrayOutputStream 并将其转换为 String 使用其中一个 String 构造函数的时候,你只需要指定你想要使用的字符编码。你只需要指定你想要使用的字符编码,在创建 PrintStream 而当把字节转换为 String.

另一种选择是用简单地将数据附加到一个名为 StringBuilder.

最后的一个选择是直接将其附加到 TextView但这需要将每项输出转换为某种类型的 CharSequence.


0
投票
public class JsonReader extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Thread(new Runnable(){ 
        public void run(){
           JSONObject json =    readJsonFromUrl("http://www.w3schools.com/json/myTutorials.txt");
    System.out.println(json.toString());
    System.out.println(json.get("display"));
     runOnUiThread(new Runnable(){ 
public void run(){
        TextView tv = (TextView) findViewById( R.id.tv );
        tv.setText(json.toString());
        tv.setText(json.get("display"));
}
});

        }
     }).start();

}

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}

public static void main(String[] args) throws IOException, JSONException {

  //move to onCreate
}
}
© www.soinside.com 2019 - 2024. All rights reserved.