【问题标题】:Cannot connect to vtiger database无法连接到 vtiger 数据库
【发布时间】:2014-07-09 07:13:12
【问题描述】:

我正在尝试实现 vtiger 的 API 以将我的 android 应用程序连接到它的服务器。我已将 API 添加到 libs 文件夹,然后编译 .jar 文件。然后我使用文档连接到 java 中的服务器。

布尔结果 = true;

WSClient client = new WSClient("http://en.vtiger.com/wip");


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



    result = client.doLogin("username", "Accesskey");

    if(!result)
    {
        System.out.println("Login failed!");
        System.out.println(client.lastError());

    }
    else
    {
        System.out.println("Login Successful");
    } 
 }

但是,我总是登录失败并且没有错误出现。问题是当我从 .jar 中打开 WSClient.java 类时,程序指出找不到源。我已经从http://forge.vtiger.com/frs/?group_id=181&release_id=573 下载了这些文件,但不知道要附加什么作为来源。也许这就是我无法连接到服务器的原因,因为我使用了 vtiger 提供的正确用户名和访问密钥。

【问题讨论】:

    标签: java android mysql android-studio vtiger


    【解决方案1】:

    试试这个:-

    这是一个基于https://demo.vtiger.com/ 登录的示例

    公共类登录扩展活动{

      //URL to get JSON Array
      private static String url = "https://demo.vtiger.com/webservice.php?operation=getchallenge&username=admin";
    
      //JSON Node Names
      private static final String TAG_RESULT = "result";
      private static final String TAG_TOKEN = "token";
    
      // contacts JSONArray
      JSONArray contacts = null;
    
      String token = null;
      String sessionId;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    
    
        new AsyncTask<Void, Void, Void>() {
    
            private ProgressDialog dialog = new ProgressDialog(Login.this);
            protected void onPreExecute() {
                   dialog.setMessage("Loging In... Please wait...");
                   dialog.show();
                  }
    @SuppressWarnings("unused")
                JSONObject result;
    
                @Override
                protected Void doInBackground(Void... params) {
    
                    // Creating new JSON Parser
                    JSONParser jParser = new JSONParser();
    
                    // Getting JSON from URL
                    JSONObject json = jParser.getJSONFromUrl(url);
    
                    try {
                        // Getting JSON Array
                        result = json.getJSONObject(TAG_RESULT);
                          JSONObject json_result = json.getJSONObject(TAG_RESULT);
    
                        // Storing  JSON item in a Variable
                        token = json_result.getString(TAG_TOKEN);
    
                        //Importing TextView
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
    
                    String username="admin";
                    String accesskeyvalue = "w9OweWKUS4a5sSL";
                    String accessKey=md5(token + accesskeyvalue);
    
                    //For debugging purpose only
                    //System.out.println(accesskeyvalue);
                    //System.out.println(token);
                    //System.out.println(accessKey);
    
            String data = null;
    
                try {
                    data = URLEncoder.encode("username", "UTF-8")
                            + "=" + URLEncoder.encode(username, "UTF-8");
                    data += "&" + URLEncoder.encode("accessKey", "UTF-8") + "="
                            + URLEncoder.encode(accessKey, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            String text = "";
            BufferedReader reader=null;
            //System.out.println(data);
    
            // Send data
            try
            {
    
                // Defined URL  where to send data
                URL url = new URL("https://demo.vtiger.com/webservice.php?operation=login");
    
             // Send POST data request
              URLConnection conn = url.openConnection();
              conn.setDoOutput(true);
              OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
              wr.write( data );
              wr.flush();    
    
            // Get the server response    
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
    
            // Read Server Response
            while((line = reader.readLine()) != null)
                {
                       // Append server response in string
                       sb.append(line + "\n");
                }
    
    
                text = sb.toString();
            }
            catch(Exception ex)
            {
    
            }
            finally
            {
                try
                {
    
                    reader.close();
                }
    
                catch(Exception ex) {}
            }
    
            // Show response
            System.out.println(text);
            sessionId = text.substring(41, 62);
            //System.out.println("doInBackground()"+sessionId);
    
    
    
        return null;    
        }
                @Override
                protected void onPostExecute(Void aVoid) {
                    super.onPostExecute(aVoid);    
                    dialog.dismiss();
                   }
    
             }.execute();
    
        } 
    
        public String md5(String s) 
        {
        MessageDigest digest;
            try 
                {
                    digest = MessageDigest.getInstance("MD5");
                    digest.update(s.getBytes(),0,s.length());
                    String hash = new BigInteger(1, digest.digest()).toString(16);
                    return hash;
                } 
            catch (NoSuchAlgorithmException e) 
                {
                    e.printStackTrace();
                }
            return "";
        }
    

    根据需要更改变量

    您不需要使用任何额外的 jar 文件。

    【讨论】:

      猜你喜欢
      • 2011-06-16
      • 2012-10-29
      • 2013-11-19
      • 2018-08-03
      • 2021-07-02
      • 2019-06-09
      相关资源
      最近更新 更多