1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| String resultz = "";
InputStream is = null ;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://tonadresse/user/login");
// Request parameters and other properties.
List<NameValuePair> params1 = new ArrayList<NameValuePair>(2);
params1.add(new BasicNameValuePair("email", email));
params1.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(params1, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
resultz=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
JSONObject obj;
try {
obj = new JSONObject(resultz);
boolean isAuth = obj.getBoolean("authenticated");
String sessionId = ((JSONObject)obj.get("user")).getString("session");
if(sessionId != null)
LocationUser.setSessionId(sessionId);
} catch (JSONException e) {
e.printStackTrace();
} |
Partager