gpt4 book ai didi

java - 将 Toast 与 BufferedReader 一起使用

转载 作者:行者123 更新时间:2023-12-02 07:23:45 24 4
gpt4 key购买 nike

希望能帮到你。

基本上,我想将“BufferedReader”读取的内容发送到 Toast,但我以前从未使用过 Toast,而且似乎无法理解它。

如下所示,BufferedReader 处于“In”状态。

提前致谢。

代码:

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

private Socket socket;
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
public String data;
public Object pd;
Intent intent;
String actu_ip;
String textStatus;

public void getModel(View view) {
try {
out.println("[m\r\n");
//System.out.print("root\r\n");
while(!in.ready());
textStatus = readBuffer();

} catch(IOException e) {
e.printStackTrace();
}
}


//get the message from intent
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
actu_ip = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
setContentView(R.layout.act_ipcontrol);

try {
new AsyncAction().execute();
} catch(Exception e) {
e.printStackTrace();
}
}

private class AsyncAction extends AsyncTask<String, Void, String> {
protected String doInBackground(String... args) {
try {
InetAddress serverAddr = InetAddress.getByName(actu_ip);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
BufferedWriter bw = new BufferedWriter(osw);
out = new PrintWriter(bw, true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (! in .ready());
readBuffer();
out.println("root\r\n");
while (! in .ready());
readBuffer();
out.println("root\r\n");
while (! in .ready());
String msg = "";
while ( in .ready()) {
msg = msg + (char) in .read();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;//returns what you want to pass to the onPostExecute()
}

protected void onPostExecute(String result) {

//results the data returned from doInbackground

IPControl.this.data = result;

}
}

private String readBuffer() throws IOException {
String msg = "";

while(in.ready()) {
msg = msg + (char)in.read();
}
//System.out.print(msg);
if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
else if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0, msg.indexOf("SCX_COM> "));
else return msg;
}
}

最佳答案

Toast 只是在屏幕上显示一条消息

Toast.makeText(上下文,要显示的消息,Toast.LENGTH_SHORT).show();

在您的 IPControl Activity 中,将其放在 onCreate 中的某个位置,您将看到 Toast 的作用

Toast.makeText(IPControl.this, "感谢您的光临!", Toast.LENGTH_LONG).show();

onPostExecute()中尝试

Toast.makeText(IPControl.this, 结果, Toast.LENGTH_LONG).show();

关于java - 将 Toast 与 BufferedReader 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13822478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com