如何在android studio

时间:2015-07-13 13:34:54

标签: java android api mikrotik

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

Mikrotik API On Github: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");

1 个答案:

答案 0 :(得分: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");
    }
}}