0

我试图从我的 Android 应用程序登录 Vtiger。我成功获得了挑战令牌,并将该挑战令牌与访问密钥(取自我的帐户首选项)结合在一起。然后转换为 MD5 字符串。

我的挑战网址:

http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=getchallenge&username=Manager

挑战回应:

{"success":true,"result":{"token":"4ff555f87eece","serverTime":1341478392,"expireTime":1341478692}}

登录网址:

http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=login&username=Manager&accesskey=976b9571ff3ff92b7786da17125ac37c

登录响应:

{"success":false,"error":{"code":"INVALID_AUTH_TOKEN","message":"Specified token is invalid or expired"}}

我也有疑问,在 Android 中,我使用 AsyncTask 进行 Http 操作。使用了两个 Asynctask。第一个用于获取挑战令牌,并使用此令牌完成第二个异步任务用于登录。是正确的方式吗?我的Android代码如下。

我的安卓代码:

public class GetChallengeActivity extends Activity {
    private TextView textView;
    private static final String USER_NAME="Manager";
    private static final String ACCESS_KEY="myaccesskey"; //taken from preferences

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void postData(View view)
    {
        textView=(TextView) findViewById(R.id.resulttext);

        ConnectivityManager conmgr=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkinfo=conmgr.getActiveNetworkInfo();
        Log.i(null, "Network Info-->"+networkinfo.toString());
        if(networkinfo!=null && networkinfo.isConnected())
        {
            //Network Available.
            //Toast.makeText(getApplicationContext(), "Network available", Toast.LENGTH_LONG).show();
            Log.i(null, "Network Available");
            textView.setText("Network Available");
            String url="http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=getchallenge&username="+USER_NAME+"";
            Log.i(null,"Challenge URL-->"+url);
            new ServerCommunication().execute(url);
        }
        else
        {
            //Network not Available
            Log.i(null, "Network Not Available");
            Toast.makeText(getApplicationContext(), "Network Not available", Toast.LENGTH_LONG).show();
            textView.setText("Network Not Available");
        }
    }

    private class ServerCommunication extends AsyncTask<String,String,String>
    {
        @Override
        protected String doInBackground(String... urls) {
            Log.i(null, "Doing in Background");
            try {
                return postToServer(urls[0]);
            }catch (IOException e) {
                Log.i(null, "Unable to retrieve web page. URL may be invalid.");
                return "Unable to retrieve web page. URL may be invalid.";

            }
        }

        @Override
        protected void onPostExecute(String result)
        {
            Log.i(null, "onPostExecute");
            Log.i(null, result);
            textView.setText(result);
        }

        private String postToServer(String string) throws IOException{
            Log.i(null, "post2Server");
            InputStream is = null;
            // Only display the first 500 characters of the retrieved web page content.
            int len = 500;

            try {
                    URL url = new URL(string);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setReadTimeout(10000 /* milliseconds */);
                    conn.setConnectTimeout(15000 /* milliseconds */);
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);

                    // Starts the query
                    conn.connect();
                    int response = conn.getResponseCode();
                    is = conn.getInputStream();

                    // Convert the InputStream into a string
                    String contentAsString = readIt(is, len);
                    Log.i(null,contentAsString);

                   String result="";
                    try {
                            JSONObject jsondata=new JSONObject(contentAsString);

                            String outcomesrf=jsondata.getString("success");
                            Log.i(null, "OutCome---->"+outcomesrf);
                            if(outcomesrf=="false")
                            {
                                JSONObject jsonobj2=jsondata.getJSONObject("error");
                                result=jsonobj2.getString("message");
                                Log.i(null,"Error-->errorMessage---->"+result);
                            }
                            else
                            {
                                JSONObject jsonobj3=jsondata.getJSONObject("result");
                                result=jsonobj3.getString("token");
                                Log.i(null,"Success-->Token---->"+result);
                                Log.i(null,"-->MD5Hash("+result+ACCESS_KEY+")");
                                String generatedkey=MD5_Hash(result+ACCESS_KEY);
                                Log.i(null,"Key-->"+generatedkey);
                                String loginurl="http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=login&username="+USER_NAME+"&accesskey="+generatedkey+"";
                                Log.i(null,"Login URL-->"+loginurl);
                                new LoginCommunication().execute(loginurl);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.i(null,"At Json "+e.toString());
                        }
                    return result;
                }
            finally {
                    if (is != null) {
                        is.close();
                    } 
                }
        }

        // Reads an InputStream and converts it to a String.

    }

    private class LoginCommunication extends AsyncTask<String,String,String>
    {
            @Override
            protected String doInBackground(String... urls) {
                Log.i(null, "Login Posting in Background");
                try {
                    return loginToServer(urls[0]);
                }catch (IOException e) {
                    Log.i(null, "Unable to retrieve web page. URL may be invalid.");
                    return "Unable to retrieve web page. URL may be invalid.";
               }
            }

            @Override
            protected void onPostExecute(String result)
            {
                Log.i(null, "LoginonPostExecute");
                Log.i(null, result);
                textView.setText(result);
            }

             private String loginToServer(String string) throws IOException{
                Log.i(null, "loginToServer");
                InputStream is = null;
                int len = 500;

                try {
                        URL url = new URL(string);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setReadTimeout(10000 /* milliseconds */);
                        conn.setConnectTimeout(15000 /* milliseconds */);
                        conn.setRequestMethod("POST");
                        conn.setDoInput(true);
                        conn.setDoOutput(true);

                        // Starts the query
                        conn.connect();
                        int response = conn.getResponseCode();
                        is = conn.getInputStream();

                        // Convert the InputStream into a string
                        String contentAsString = readIt(is, len);
                        Log.i(null,contentAsString);

                       String result1="";
                       String result2="";

                        try {
                                JSONObject jsondata=new JSONObject(contentAsString);

                                String outcomesrf=jsondata.getString("success");
                                Log.i(null, "Login OutCome---->"+outcomesrf);
                                if(outcomesrf=="false")
                                {
                                    JSONObject jsonobj2=jsondata.getJSONObject("error");
                                    result1=jsonobj2.getString("message");
                                    Log.i(null,"Error-->errorMessage---->"+result1);
                                }
                                else
                                {
                                    JSONObject jsonobj3=jsondata.getJSONObject("result");
                                    result1=jsonobj3.getString("sessionName");
                                    Log.i(null,"Success-->sessionName---->"+result1);

                                    JSONObject jsonobj4=jsondata.getJSONObject("result");
                                    result2=jsonobj4.getString("userId");
                                    Log.i(null,"Success-->userId---->"+result2);

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Log.i(null,"At Json "+e.toString());
                            }
                        String result="sessionName"+result1+" userId"+result2;
                        return result;
                    }
                finally {
                        if (is != null) {
                            is.close();
                        } 
                    }
            }
        }

        public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
            Reader reader = null;
            reader = new InputStreamReader(stream, "UTF-8");        
            char[] buffer = new char[len];
            reader.read(buffer);
            return new String(buffer);
        }

        public static String MD5_Hash(String s) {
            MessageDigest m = null;

            try {
                    m = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
            }

            m.update(s.getBytes(),0,s.length());
            String hash = new BigInteger(1, m.digest()).toString(16);
            return hash;
        }

}
4

1 回答 1

2

在您的 PostToServer 中,accesskey 动词应该带有一个大写 k (accessKey)

String loginurl="http://mysite/vtigerCRM510-RC/vtigerCRM/webservice.php?operation=login&username="+USER_NAME+"&accessKey="+generatedkey+"";
于 2012-08-25T00:56:09.133 回答