2

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

Github 上的 Mikrotik API:https ://github.com/GideonLeGrange/mikrotik-java

在 MainActivity 的乞求中导入

import me.legrange.mikrotik.MikrotikApiException;
import me.legrange.mikrotik.ApiConnection;
import me.legrange.mikrotik.ResultListener;

我要连接的 java 代码:

ApiConnection con = ApiConnection.connect("10.0.1.1");

4

1 回答 1

1

例子:

public class MainActivity extends Activity implements View.OnClickListener{
final String LOG_TAG = "mLog";    

Button btnConnect;

MyTask mt;

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

    btnConnect = (Button) findViewById(R.id.btnConnect);     

    btnConnect.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
    mt = new MyTask();
    mt.execute();
}

class MyTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tvResult.setText("Begin");
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {

            List<Map<String, String>> result = null;                

            try
            {                    
                Log.d(LOG_TAG, "start");

                ApiConnection con = ApiConnection.connect("IP");
                Log.d(LOG_TAG, "start2");
                con.login("login", "password");
                if(con.isConnected())
                {
                    //tvResult.setText("OK!");
                    Log.d(LOG_TAG, "isConnected");
                }                    
                result = con.execute("/interface/print");
                for(Map<String, String> res : result)
                {                       
                    Log.d(LOG_TAG, res.toString());
                }
                con.close();                   
            }
            catch (Exception e)
            {                   
                Log.d(LOG_TAG, "error");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        tvResult.setText("End");
    }
}}
于 2016-03-30T11:59:13.057 回答