Results 1 to 3 of 3

Thread: REST call from Java (Android)

  1. #1
    Makica is offline Junior Member
    Join Date
    Jul 2010
    Posts
    1

    Default REST call from Java (Android)

    I am new in these things, so I need help to connect to sugarCRM web service from my Android application.
    Can anyone give me some examples how to connect to REST web service from Java?

    Thanks in advance,
    Marina

  2. #2
    kbandi is offline Junior Member
    Join Date
    Jul 2010
    Posts
    2

    Thumbs up Re: REST call from Java (Android)

    Hey Marina,

    I am new to this too.. but jus found a way to get through... Probably the code should be helpful...

    public String login(String userName, String password, String baseURL) throws Exception
    {
    String password1 = encryptor(password);

    JSONObject jso = new JSONObject();
    jso.put("user_name", userName);
    jso.put("password", password1);

    JSONObject jso2 = new JSONObject();
    jso2.put("user_auth", jso);
    jso2.put("application", "application_name");

    String mockdata = jso2.toString();
    String data = null;
    String baseurl = baseURL;

    data = httpPost(baseurl+"?method=login&input_type=json&re sponse_type=json&rest_data="+mockdata);

    JSONObject jsondata = (JSONObject)JSONSerializer.toJSON(data);
    String sessionid = jsondata.getString("id");

    return sessionid;
    }

    Method httpPost : ---

    public static String httpPost(String urlStr) throws Exception {
    URL url = new URL(urlStr);
    HttpURLConnection conn =
    (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setAllowUserInteraction(false);
    conn.setRequestProperty("Content-Type",
    "application/x-www-form-urlencoded");

    if (conn.getResponseCode() != 200) {
    throw new IOException(conn.getResponseMessage());
    }

    // Buffer the result into a string
    BufferedReader rd = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = rd.readLine()) != null) {
    sb.append(line);
    }
    rd.close();

    conn.disconnect();
    return sb.toString();
    }

    Encryptor for password :

    public String encryptor(String password)
    {
    String pwd = password;

    String temppass = null;

    byte[] defaultBytes = pwd.getBytes();
    try{
    MessageDigest algorithm = MessageDigest.getInstance("MD5");
    algorithm.reset();
    algorithm.update(defaultBytes);
    byte messageDigest[] = algorithm.digest();

    StringBuffer hexString = new StringBuffer();
    for (int i=0;i<messageDigest.length;i++) {
    hexString.append(String.format("%02x", 0xFF & messageDigest[i])); // formatting to have the leading zeros
    }
    temppass = hexString.toString();
    }catch(NoSuchAlgorithmException nsae){
    System.out.println("No Such Algorithm found");
    }

    return temppass;
    }


    Cheers....
    -Karthik...

  3. #3
    lovell is offline Junior Member
    Join Date
    Aug 2010
    Posts
    4

    Default Re: REST call from Java (Android)

    I've tried to use what you've put up, but I don't get a JSON reply, only HTML, and with no 'id' value that comes from the PHP script (or anything else other than a sign-in URL screen).

    'listing 2' from http://www.ibm.com/developerworks/li...est/index.html with a couple of modifications works well, but can you elaborate on what you're doing here, have you got it to work fully in Java?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. SugarCRM + Android => done! Anyone interested ?
    By Ricau in forum General Discussion
    Replies: 11
    Last Post: 2010-11-03, 03:44 PM
  2. using Sprint HTC Android phone
    By henrype in forum General Discussion
    Replies: 2
    Last Post: 2010-04-14, 11:52 AM
  3. Sugar app for the Droid Google Phone or Android software
    By ntroxell in forum Feature Requests
    Replies: 2
    Last Post: 2009-12-24, 09:55 PM
  4. Replies: 7
    Last Post: 2009-03-20, 11:36 PM
  5. Java or ASP call sugarcrm web service to return a record
    By ahmay8577 in forum Developer Help
    Replies: 5
    Last Post: 2009-02-05, 05:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •