aboutsummaryrefslogtreecommitdiff
path: root/src/org/happysanta/gd/API/Request.java
blob: d8bfa6e066887365fbc96b32fceff5a874bed768 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.happysanta.gd.API;

import android.os.AsyncTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Locale;

import static org.happysanta.gd.Helpers.getAppVersion;
import static org.happysanta.gd.Helpers.logDebug;

public class Request {

	// enum Method { GET, POST };

	private List<NameValuePair> params;
	private ResponseHandler handler;
	private AsyncRequestTask task;
	private String apiURL;

	public Request(String method, List<NameValuePair> params, ResponseHandler handler, boolean useDebugURL) {
		construct(method, params, handler, useDebugURL ? API.DEBUG_URL : API.URL);
	}

	public Request(String method, List<NameValuePair> params, ResponseHandler handler) {
		construct(method, params, handler, API.URL);
	}

	private void construct(String method, List<NameValuePair> params, ResponseHandler handler, String apiURL) {
		this.apiURL = apiURL;

		params.add(new BasicNameValuePair("v", String.valueOf(API.VERSION)));
		params.add(new BasicNameValuePair("method", method));
		params.add(new BasicNameValuePair("app_version", getAppVersion()));
		params.add(new BasicNameValuePair("app_lang", Locale.getDefault().getDisplayLanguage()));

		this.params = params;
		this.handler = handler;

		go();
	}

	private void go() {
		task = new AsyncRequestTask();
		task.execute(apiURL);
	}

	public void cancel() {
		if (task != null) {
			task.cancel(true);
			task = null;
		}
	}

	private void onDone(String result) {
		Response response;
		logDebug("API.Request.onDone()");

		try {
			response = new Response(result);
		} catch (APIException e) {
			handler.onError(e);
			return;
		} catch (Exception e) {
			// e.printStackTrace();
			handler.onError(new APIException(result == null ? "Network error" : "JSON parsing error"));
			return;
			// exception = new Exception();
		}

		// handler.onResponse(response);

		if (response != null)
			handler.onResponse(response);
		else
			handler.onError(new APIException("JSON parsing error"));
	}

	protected class AsyncRequestTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... objects) {
			String url = objects[0];

			DefaultHttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			try {
				post.setEntity(new UrlEncodedFormEntity(params));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return null;
			}
			String result = null;
			InputStream is = null;

			try {
				HttpResponse response = client.execute(post);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

				BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
				StringBuilder sb = new StringBuilder();

				String line;
				while ((line = reader.readLine()) != null) {
					if (isCancelled()) break;
					sb.append(line + "\n");
				}
				result = sb.toString();
			} catch (java.lang.Exception e) {
				logDebug("API request failed: " + e.getMessage());
				// e.printStackTrace();
				return null;
			} finally {
				try {
					if (is != null) is.close();
				} catch (IOException e) {
					// e.printStackTrace();
				}
			}

			return result;
		}

		@Override
		public void onPostExecute(String result) {
			onDone(result);
		}

	}

}