package com.sjriley.zappit.models; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import android.content.Context; import android.location.Location; import android.util.Log; import com.google.gson.Gson; import com.sjriley.zappit.services.LocationService; import com.sjriley.zappit.vo.LoginRequest; import com.sjriley.zappit.vo.LoginResponse; public class LoginModel extends BaseModel { private static final String TAG = LoginModel.class.getSimpleName(); public LoginModel(Context context){ super(context); } public LoginResponse login(String code, String email, String password) { HttpPost request = new HttpPost("http://zappit.co/service/login"); List<NameValuePair> nvp = new ArrayList<NameValuePair>(); Location location = getLocation(); LoginRequest params = new LoginRequest(code, email, password, location.getLatitude(),location.getLongitude()); Gson gson = new Gson(); nvp.add(new BasicNameValuePair("info", gson.toJson(params))); nvp.add(new BasicNameValuePair("submit", "send")); LoginResponse result = new LoginResponse(); try { request.setEntity(new UrlEncodedFormEntity(nvp, HTTP.UTF_8)); HttpResponse response = httpClient.execute(request); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); result = gson.fromJson(reader, LoginResponse.class); } catch (UnsupportedEncodingException e) { result.setMessage("Could not encode parameters"); e.printStackTrace(); } catch (ClientProtocolException e) { result.setMessage("Could not connect to server"); e.printStackTrace(); } catch (IOException e) { result.setMessage("Could not read response"); e.printStackTrace(); } return result; } }