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
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
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...
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?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks