我目前正在使用我的第一个应用程序,并且我使用了try和catch代码,所以我给了Log.e()
来显示错误消息。所以我问如何将Log.e()
错误显示为Toast
?
这里我附上了使用JSCH的代码
package beny.com.solehap;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
String status;
private Boolean IsRunning;
private String eror;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.btntest);
Button login = (Button) findViewById(R.id.login);
Button keluar = (Button) findViewById(R.id.btnexit);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new sshkonek().execute();
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent login = new Intent(MainActivity.this, login.class);
startActivity(login);
}
});
keluar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
close();
}
});
}
public void close(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Sure to exit?").setCancelable(false).setPositiveButton("Ya", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
}).setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
public void konek() {
sshkonek task = new sshkonek();
}
public class sshkonek extends AsyncTask<String, Void, String>{
ByteArrayOutputStream baos= new ByteArrayOutputStream();
ByteArrayInputStream bais=new ByteArrayInputStream(new byte[1000]);
ProgressDialog loading;
@Override
protected void onPreExecute() {
loading = new ProgressDialog(MainActivity.this);
loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
loading.setMessage("Memproses.. Mohon Tunggu");
loading.setCancelable(true);
loading.setMax(100);
loading.show();
}
@Override
protected String doInBackground(String... params) {
String host = "192.168.197.101";
String user = "pi";
String pwd = "raspberry";
int port = 22;
JSch js = new JSch();
try{
Session sesi = js.getSession(user, host, port);
sesi.setPassword(pwd);
sesi.setConfig("StrictHostKeyChecking", "no");
sesi.connect(100);
ChannelExec kanal = (ChannelExec)sesi.openChannel("exec");
BufferedReader in=new BufferedReader(new InputStreamReader(kanal.getInputStream()));
kanal.setOutputStream(baos);
kanal.setInputStream(bais);
//koman
kanal.setCommand("ls");
kanal.connect();
publishProgress();
try {
Thread.sleep(3500);
}catch (Exception ignored){};
kanal.disconnect();
}catch (Exception e){
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
e.printStackTrace();
return "ERROR_IN_CODE";
}
return baos.toString();
}
@Override
protected void onPostExecute(String unused) {
if (unused != null && unused.equals("ERROR_IN_CODE")){
Toast.makeText(MainActivity.this, "Error"+ Log.e(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Sukses", Toast.LENGTH_SHORT).show();
}
loading.dismiss();
}
}
}
答案 0 :(得分:0)
Beny Putra Petir你可以通过这段代码来做到这一点
catch (Exception e){
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
eror=e.toString();
e.printStackTrace();
return "ERROR_IN_CODE";
}
和吐司
Toast.makeText(MainActivity.this, "Error "+ eror, Toast.LENGTH_SHORT).show();