This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttpPost.java
95 lines (76 loc) · 3.63 KB
/
HttpPost.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// TODO: link user object (picture, restaurant catagory picked, lat, long, unique id)
public JSONArray getRestaurantArray() {
int radius = 3;
HttpClient httpClient = new DefaultHttpClient();
String url = "http://api.tripadvisor.com/api/partner/1.0/location/60745/restaurants?key=92C34F58BB4F4E03894F5D171B79857E&cuisines=" + restaurantCategory;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String output = EntityUtils.toString(httpEntity);
JSONObject dataObject = new JSONObject(output);
JSONArray restaurants = dataObject.getJSONArray(data);
JSONArray distSortedRestaurants = new JSONArray();
for(JSONObject restaurant : restaurants) {
if(distanceBetween(restaurant.latitude, restaurant.longitude, user.latitude, user.longitude) < radius) {
distSortedRestaurants.add(restaurant);
}
}
if(distSortedRestaurants.size() > 5) {
for(i=distSortedRestaurants.size(); i==5; i--) {
Random rand = new Random();
int x = rand.nextInt(distSortedRestaurants.size());
distSortedRestaurants.remove(x);
}
}
return distSortedRestaurants;
}
public double toRad(double degrees) {
return degrees * Math.PI / 180;
}
public double distanceBetween(double lat1, double long1, double lat2, double long2) {
double EARTH_RADIUS = 3963.1906;
double dLat = toRad(lat1 - lat2);
double dLon = toRad(long1 - long2);
double l1 = toRad(lat1);
double l2 = toRad(lat2);
double a = (Math.sin(dLat/2) * Math.sin(dLat/2)) + (Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(l1) * Math.cos(l2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return EARTH_RADIUS * c;
}
public void postDataBeforeRestaurant() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("172.16.7.237:8080");
try {
// Add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("restaurantId", "45678")); /// Restaurant ID
nameValuePairs.add(new BasicNameValuePair("duration", "35 + 56")); // systime + time frame
nameValuePairs.add(new BasicNameValuePair("5084983232")); // systime + time frame
nameValuePairs.add(new BasicNameValuePair("id", "12345")); /// FB
nameValuePairs.add(new BasicNameValuePair("longitude", "34")); // long
nameValuePairs.add(new BasicNameValuePair("latitude", "34")); // lat
nameValuePairs.add(new BasicNameValuePair("avatarUrl", "graph.facebook.com/username/picture")); //picture
nameValuePairs.add(new BasicNameValuePair("foodPreference", "Italian")); //picture
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request (Response = List of people)
HttpResponse response = httpclient.execute(httppost);
String responseString = inputStreamToString(response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
return total;
}