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.vo.EmailRequest;
import com.sjriley.zappit.vo.EmailResponse;
public class EmailModel extends BaseModel
{
private static final String TAG = EmailModel.class.getSimpleName();
public EmailModel(Context context){
super(context);
}
public EmailResponse emailFriend(String userId, String email) {
HttpPost request = new HttpPost("http://zappit.co/service/sendtofriend");
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
Location location = getLocation();
Log.d(TAG, "lat =" + location.getLatitude());
Log.d(TAG, "lat =" + location.getLongitude());
EmailRequest params = new EmailRequest("F9kmN27a",userId,email,location.getLatitude(), location.getLongitude());
Gson gson = new Gson();
nvp.add(new BasicNameValuePair("info", gson.toJson(params)));
nvp.add(new BasicNameValuePair("submit", "send"));
EmailResponse result = new EmailResponse();
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, EmailResponse.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;
}
}