如何在Android Studio上使用Mikrotik API

时间:2019-03-03 11:07:24

标签: 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[] 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) {
        }
    });
}

我收到此消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.duaempatsinergi.sams.teknisi24, PID: 5947
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:333)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
    at java.net.Socket.connect(Socket.java:586)
    at me.legrange.mikrotik.impl.ApiConnectionImpl.open(ApiConnectionImpl.java:142)
    at me.legrange.mikrotik.impl.ApiConnectionImpl.connect(ApiConnectionImpl.java:44)
    at me.legrange.mikrotik.ApiConnection.connect(ApiConnection.java:48)
    at com.duaempatsinergi.sams.teknisi24.MainActivity$1.onItemSelected(MainActivity.java:40)
    at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
    at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
    at android.widget.AdapterView.-wrap1(AdapterView.java)
    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

与目标VM断开连接,地址:“ localhost:8620”,传输:“ socket”

我在这里做错了什么?我不是Android开发人员,但试图为我工作过的公司开发有用的东西。

1 个答案:

答案 0 :(得分:0)

在android中,您无法在主线程上运行与网络相关的操作-您需要在其他线程上运行它们,例如:

Thread yourThread = new Thread(new Runnable() {

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

yourThread .start();