Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android Studio上使用Mikrotik API_Java_Android_Api_Mikrotik - Fatal编程技术网

Java 如何在Android Studio上使用Mikrotik API

Java 如何在Android Studio上使用Mikrotik API,java,android,api,mikrotik,Java,Android,Api,Mikrotik,我试图在android中使用mikrotik api Java,但当我下载并添加到android studio项目中并使用它时,应用程序被迫关闭 这是我的代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String[] str

我试图在android中使用mikrotik api Java,但当我下载并添加到android studio项目中并使用它时,应用程序被迫关闭

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final String[] string_array = getResources().getStringArray(R.array.ip_venue);
    spinner2 = findViewById(R.id.list_venue);
    spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View view, int position, long id12) {
                String Host1 = String.valueOf(string_array[arg0.getSelectedItemPosition()]);
            ApiConnection con = null; // connect to router
            try {
                con = ApiConnection.connect(SocketFactory.getDefault(), Host1, PORT, 2000);
            } catch (MikrotikApiException e) {
                e.printStackTrace();
            }
            try {
                con.login(USERNAME,PASSWORD); // log in to router
            } catch (MikrotikApiException e) {
                e.printStackTrace();
            }
            try {
                con.execute("/system/reboot"); // execute a command
            } catch (MikrotikApiException e) {
                e.printStackTrace();
            }
            try {
                con.close(); // disconnect from router
            } catch (ApiConnectionException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
}
已断开与目标VM的连接,地址:'localhost:8620',传输:'socket'


我做错了什么?我不是安卓开发者,但我想为我工作的公司做些有用的事情。

在安卓系统中,你不能在主线程上运行与网络相关的操作-你需要在不同的线程上运行它们,例如:

Thread yourThread = new Thread(new Runnable() {

@Override
public void run() {
    try  {
        //Put your code in here
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
});

yourThread .start(); 
Thread yourThread = new Thread(new Runnable() {

@Override
public void run() {
    try  {
        //Put your code in here
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
});

yourThread .start();