fork download
  1. package com.cramdroid.api;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.HttpRetryException;
  7. import java.net.URI;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.ParseException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpGet;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.client.methods.HttpUriRequest;
  14. import org.apache.http.entity.mime.HttpMultipartMode;
  15. import org.apache.http.entity.mime.MultipartEntity;
  16. import org.apache.http.entity.mime.content.ByteArrayBody;
  17. import org.apache.http.entity.mime.content.FileBody;
  18. import org.apache.http.entity.mime.content.StringBody;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.util.EntityUtils;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import android.content.Context;
  25. import android.net.Uri;
  26. import android.util.Log;
  27. import android.widget.Toast;
  28.  
  29. public abstract class HttpHandler {
  30.  
  31. private static String TAG = HttpHandler.class.getSimpleName();
  32.  
  33. // private String url;
  34. private String apiPath;
  35. private final HttpUriRequest request;
  36. // private ArrayList<Pair<String, String>> params;
  37. // private MultipartEntity entity;
  38. private static HttpClient httpClient = null;
  39.  
  40. private static HttpClient getHttpClient() {
  41. if (httpClient == null) {
  42. httpClient = new DefaultHttpClient();
  43. }
  44. return httpClient;
  45. }
  46.  
  47. protected HttpHandler(String apiPath, HttpUriRequest request) {
  48. this.apiPath = apiPath;
  49. this.request = request;
  50. }
  51.  
  52. public final String getApiPath() {
  53. return apiPath;
  54. }
  55.  
  56. public final HttpResponse getResponse() {
  57.  
  58. HttpUriRequest request = buildHttpUriRequest();
  59. if (request == null) {
  60. return null;
  61. }
  62. HttpResponse response = null;
  63. try {
  64. response = getHttpClient().execute(request);
  65. } catch (IOException e) {
  66. Log.w(TAG, "apiPath"+this.apiPath);
  67. Log.w(TAG, "HttpClient exec, IOException", e);
  68. }
  69. return response;
  70. }
  71.  
  72. public final JSONObject getResult() throws HttpRetryException {
  73. return getResultFromResponse(getResponse());
  74. }
  75.  
  76. public void addHeader(String name, String value) {
  77. request.addHeader(name, value);
  78. }
  79.  
  80. public abstract boolean addParam(String key, String value);
  81.  
  82. public abstract HttpUriRequest buildHttpUriRequest();
  83.  
  84. public static final class GetHttpHandler extends HttpHandler {
  85. private Uri.Builder builder;
  86.  
  87. public GetHttpHandler(String path, boolean isFullUrl) {
  88. super(path, new HttpGet());
  89. builder = Uri.parse((isFullUrl ? "" : APIConstants.SERVER_HOST) + path)
  90. .buildUpon();
  91. }
  92.  
  93. public GetHttpHandler(String apiPath) {
  94. this(apiPath, false);
  95. }
  96.  
  97. @Override
  98. public boolean addParam(String key, String value) {
  99. this.builder.appendQueryParameter(key, value);
  100. return false;
  101. }
  102.  
  103. @Override
  104. public HttpUriRequest buildHttpUriRequest() {
  105. HttpGet get = (HttpGet) super.request;
  106. Log.d(TAG,"GetHttpHandler PATH="+this.builder.build().toString());
  107. get.setURI(URI.create(this.builder.build().toString()));
  108. return get;
  109. }
  110. }
  111.  
  112. public static final class PostHttpHandler extends HttpHandler {
  113.  
  114. private MultipartEntity multipart;
  115.  
  116. public PostHttpHandler(String path, boolean isFullUrl) {
  117. super(path, new HttpPost((isFullUrl ? "" : APIConstants.SERVER_HOST) + path));
  118. multipart = new MultipartEntity(
  119. HttpMultipartMode.BROWSER_COMPATIBLE);
  120. }
  121.  
  122. public PostHttpHandler(String apiPath) {
  123. this(apiPath, false);
  124. }
  125.  
  126. @Override
  127. public boolean addParam(String key, String value) {
  128. try {
  129. multipart.addPart(key, new StringBody(value));
  130. return false;
  131. }
  132. return true;
  133. }
  134.  
  135. @Override
  136. public HttpUriRequest buildHttpUriRequest() {
  137. Log.d(TAG,"GetHttpHandler PATH="+super.request.getURI().getPath());
  138. HttpPost post = (HttpPost) super.request;
  139. post.setEntity(multipart);
  140. return post;
  141. }
  142.  
  143. public void addByte(String key, byte[] value, String fileName) {
  144. multipart.addPart(key, new ByteArrayBody(value, fileName));
  145. }
  146.  
  147. public void addByte(String key, byte[] value) {
  148. addByte(key, value, "FilenameUnknown");
  149. }
  150.  
  151. public void addFile(String key, File value) {
  152. multipart.addPart(key, new FileBody(value));
  153. }
  154. }
  155.  
  156. /**
  157. * @return JSONObject , JSONArray
  158. * @param response
  159. * Get statuscode and stringbody form this HttpResponse
  160. * @throws IOException
  161. * @throws ParseException
  162. * @throws JSONException
  163. */
  164.  
  165. public static JSONObject getResultFromResponse(HttpResponse response)
  166. throws HttpRetryException {
  167. if (response == null)
  168. return null;
  169. String res = null;
  170. try {
  171. res = EntityUtils.toString(response.getEntity());
  172. } catch (ParseException e) {
  173. Log.w(TAG, "getResultFromResponse, ParseException", e);
  174. } catch (IOException e) {
  175. Log.w(TAG, "getResultFromResponse, IOException", e);
  176. }
  177. if (res == null)
  178. return null;
  179. int statusCode = response.getStatusLine().getStatusCode();
  180. Log.d(TAG, "getResultFromResponse: " + res);
  181. Log.d(TAG, "statusCode: " + statusCode);
  182.  
  183. JSONObject obj = null;
  184. try {
  185. obj = new JSONObject(res);
  186. } catch (JSONException e) {
  187. Log.w(TAG, "getResultFromResponse, JSONException", e);
  188. }
  189. if (obj == null)
  190. return null;
  191.  
  192. if (statusCode <= 300) {
  193. return obj;
  194. } else if (statusCode >= 400) {
  195. String error = null;
  196. try {
  197. error = obj.getString("error");
  198. } catch (JSONException e) {
  199. Log.w(TAG, "getResultFromResponse, JSONException", e);
  200. }
  201. throw new HttpRetryException(error, statusCode);
  202. }
  203. return null;
  204. }
  205.  
  206. /**
  207. * TODO
  208. *
  209. * @param ctx
  210. */
  211. public static void AlertNoNetwork(Context ctx) {
  212. Toast.makeText(ctx, "Alert No Network!!!!!", 0).show();
  213. Log.d(TAG, "Alert No Network!!!!!");
  214. }
  215.  
  216. /**
  217. * TODO
  218. *
  219. * @return always return true
  220. * @param ctx
  221. */
  222.  
  223. public static boolean checkNetworkStatus(Context ctx) {
  224. Log.d("Network", "Checking Network... (Without Checking!!!!!)");
  225.  
  226. // HttpClient httpClient = new DefaultHttpClient();
  227. // HttpGet httpGet = new HttpGet("http://www.google.com");
  228. //
  229. // try {
  230. // httpClient.execute(httpGet);
  231. // } catch (Exception e) {
  232. // Log.d("Network", "Checking Network Done!!");
  233. // return false;
  234. // }
  235.  
  236. Log.d("Network", "Checking Network Done!!");
  237. return true;
  238.  
  239. // ConnectivityManager cm = (ConnectivityManager)
  240. // context.getSystemService(Context.CONNECTIVITY_SERVICE);
  241. // NetworkInfo netInfo = cm.getActiveNetworkInfo();
  242. //
  243. // if (netInfo!=null && netInfo.isConnected()) {
  244. // return true;
  245. // }
  246. //
  247. // return false;
  248. }
  249.  
  250. }
  251.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:29: class HttpHandler is public, should be declared in a file named HttpHandler.java
public abstract class HttpHandler {
                ^
Main.java:8: package org.apache.http does not exist
import org.apache.http.HttpResponse;
                      ^
Main.java:9: package org.apache.http does not exist
import org.apache.http.ParseException;
                      ^
Main.java:10: package org.apache.http.client does not exist
import org.apache.http.client.HttpClient;
                             ^
Main.java:11: package org.apache.http.client.methods does not exist
import org.apache.http.client.methods.HttpGet;
                                     ^
Main.java:12: package org.apache.http.client.methods does not exist
import org.apache.http.client.methods.HttpPost;
                                     ^
Main.java:13: package org.apache.http.client.methods does not exist
import org.apache.http.client.methods.HttpUriRequest;
                                     ^
Main.java:14: package org.apache.http.entity.mime does not exist
import org.apache.http.entity.mime.HttpMultipartMode;
                                  ^
Main.java:15: package org.apache.http.entity.mime does not exist
import org.apache.http.entity.mime.MultipartEntity;
                                  ^
Main.java:16: package org.apache.http.entity.mime.content does not exist
import org.apache.http.entity.mime.content.ByteArrayBody;
                                          ^
Main.java:17: package org.apache.http.entity.mime.content does not exist
import org.apache.http.entity.mime.content.FileBody;
                                          ^
Main.java:18: package org.apache.http.entity.mime.content does not exist
import org.apache.http.entity.mime.content.StringBody;
                                          ^
Main.java:19: package org.apache.http.impl.client does not exist
import org.apache.http.impl.client.DefaultHttpClient;
                                  ^
Main.java:20: package org.apache.http.util does not exist
import org.apache.http.util.EntityUtils;
                           ^
Main.java:21: package org.json does not exist
import org.json.JSONException;
               ^
Main.java:22: package org.json does not exist
import org.json.JSONObject;
               ^
Main.java:24: package android.content does not exist
import android.content.Context;
                      ^
Main.java:25: package android.net does not exist
import android.net.Uri;
                  ^
Main.java:26: package android.util does not exist
import android.util.Log;
                   ^
Main.java:27: package android.widget does not exist
import android.widget.Toast;
                     ^
Main.java:35: cannot find symbol
symbol  : class HttpUriRequest
location: class com.cramdroid.api.HttpHandler
	private final HttpUriRequest request;
	              ^
Main.java:38: cannot find symbol
symbol  : class HttpClient
location: class com.cramdroid.api.HttpHandler
	private static HttpClient httpClient = null;
	               ^
Main.java:40: cannot find symbol
symbol  : class HttpClient
location: class com.cramdroid.api.HttpHandler
	private static HttpClient getHttpClient() {
	               ^
Main.java:47: cannot find symbol
symbol  : class HttpUriRequest
location: class com.cramdroid.api.HttpHandler
	protected HttpHandler(String apiPath, HttpUriRequest request) {
	                                      ^
Main.java:56: cannot find symbol
symbol  : class HttpResponse
location: class com.cramdroid.api.HttpHandler
	public final HttpResponse getResponse() {
	             ^
Main.java:72: cannot find symbol
symbol  : class JSONObject
location: class com.cramdroid.api.HttpHandler
	public final JSONObject getResult() throws HttpRetryException {
	             ^
Main.java:82: cannot find symbol
symbol  : class HttpUriRequest
location: class com.cramdroid.api.HttpHandler
	public abstract HttpUriRequest buildHttpUriRequest();
	                ^
Main.java:166: cannot find symbol
symbol  : class HttpResponse
location: class com.cramdroid.api.HttpHandler
	public static JSONObject getResultFromResponse(HttpResponse response)
	                                               ^
Main.java:166: cannot find symbol
symbol  : class JSONObject
location: class com.cramdroid.api.HttpHandler
	public static JSONObject getResultFromResponse(HttpResponse response)
	              ^
Main.java:212: cannot find symbol
symbol  : class Context
location: class com.cramdroid.api.HttpHandler
	public static void AlertNoNetwork(Context ctx) {
	                                  ^
Main.java:224: cannot find symbol
symbol  : class Context
location: class com.cramdroid.api.HttpHandler
	public static boolean checkNetworkStatus(Context ctx) {
	                                         ^
Main.java:85: package Uri does not exist
		private Uri.Builder builder;
		           ^
Main.java:104: cannot find symbol
symbol  : class HttpUriRequest
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
		public HttpUriRequest buildHttpUriRequest() {
		       ^
Main.java:114: cannot find symbol
symbol  : class MultipartEntity
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
		private MultipartEntity multipart;
		        ^
Main.java:137: cannot find symbol
symbol  : class HttpUriRequest
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
		public HttpUriRequest buildHttpUriRequest() {
		       ^
Main.java:42: cannot find symbol
symbol  : class DefaultHttpClient
location: class com.cramdroid.api.HttpHandler
			httpClient = new DefaultHttpClient();
			                 ^
Main.java:58: cannot find symbol
symbol  : class HttpUriRequest
location: class com.cramdroid.api.HttpHandler
		HttpUriRequest request = buildHttpUriRequest();
		^
Main.java:62: cannot find symbol
symbol  : class HttpResponse
location: class com.cramdroid.api.HttpHandler
		HttpResponse response = null;
		^
Main.java:66: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
			Log.w(TAG, "apiPath"+this.apiPath);
			^
Main.java:67: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
			Log.w(TAG, "HttpClient exec, IOException", e);
			^
Main.java:88: cannot find symbol
symbol  : class HttpGet
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
			super(path, new HttpGet());
			                ^
Main.java:89: cannot find symbol
symbol  : variable APIConstants
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
			builder = Uri.parse((isFullUrl ? "" : APIConstants.SERVER_HOST) + path)
			                                      ^
Main.java:89: cannot find symbol
symbol  : variable Uri
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
			builder = Uri.parse((isFullUrl ? "" : APIConstants.SERVER_HOST) + path)
			          ^
Main.java:105: cannot find symbol
symbol  : class HttpGet
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
			HttpGet get = (HttpGet) super.request;
			^
Main.java:105: cannot find symbol
symbol  : class HttpGet
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
			HttpGet get = (HttpGet) super.request;
			               ^
Main.java:106: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler.GetHttpHandler
			Log.d(TAG,"GetHttpHandler PATH="+this.builder.build().toString());
			^
Main.java:117: cannot find symbol
symbol  : class HttpPost
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			super(path, new HttpPost((isFullUrl ? "" : APIConstants.SERVER_HOST) + path));
			                ^
Main.java:117: cannot find symbol
symbol  : variable APIConstants
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			super(path, new HttpPost((isFullUrl ? "" : APIConstants.SERVER_HOST) + path));
			                                           ^
Main.java:118: cannot find symbol
symbol  : class MultipartEntity
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			multipart = new MultipartEntity(
			                ^
Main.java:119: cannot find symbol
symbol  : variable HttpMultipartMode
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
					HttpMultipartMode.BROWSER_COMPATIBLE);
					^
Main.java:129: cannot find symbol
symbol  : class StringBody
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
				multipart.addPart(key, new StringBody(value));
				                           ^
Main.java:138: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			Log.d(TAG,"GetHttpHandler PATH="+super.request.getURI().getPath());
			^
Main.java:139: cannot find symbol
symbol  : class HttpPost
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			HttpPost post = (HttpPost) super.request;
			^
Main.java:139: cannot find symbol
symbol  : class HttpPost
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			HttpPost post = (HttpPost) super.request;
			                 ^
Main.java:145: cannot find symbol
symbol  : class ByteArrayBody
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			multipart.addPart(key, new ByteArrayBody(value, fileName));
			                           ^
Main.java:153: cannot find symbol
symbol  : class FileBody
location: class com.cramdroid.api.HttpHandler.PostHttpHandler
			multipart.addPart(key, new FileBody(value));
			                           ^
Main.java:172: cannot find symbol
symbol  : variable EntityUtils
location: class com.cramdroid.api.HttpHandler
			res = EntityUtils.toString(response.getEntity());
			      ^
Main.java:173: cannot find symbol
symbol  : class ParseException
location: class com.cramdroid.api.HttpHandler
		} catch (ParseException e) {
		         ^
Main.java:174: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
			Log.w(TAG, "getResultFromResponse, ParseException", e);
			^
Main.java:176: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
			Log.w(TAG, "getResultFromResponse, IOException", e);
			^
Main.java:181: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
		 Log.d(TAG, "getResultFromResponse: " + res);
		 ^
Main.java:182: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
		 Log.d(TAG, "statusCode: " + statusCode);
		 ^
Main.java:184: cannot find symbol
symbol  : class JSONObject
location: class com.cramdroid.api.HttpHandler
		JSONObject obj = null;
		^
Main.java:186: cannot find symbol
symbol  : class JSONObject
location: class com.cramdroid.api.HttpHandler
			obj = new JSONObject(res);
			          ^
Main.java:187: cannot find symbol
symbol  : class JSONException
location: class com.cramdroid.api.HttpHandler
		} catch (JSONException e) {
		         ^
Main.java:188: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
			Log.w(TAG, "getResultFromResponse, JSONException", e);
			^
Main.java:199: cannot find symbol
symbol  : class JSONException
location: class com.cramdroid.api.HttpHandler
			} catch (JSONException e) {
			         ^
Main.java:200: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
				Log.w(TAG, "getResultFromResponse, JSONException", e);
				^
Main.java:213: cannot find symbol
symbol  : variable Toast
location: class com.cramdroid.api.HttpHandler
		Toast.makeText(ctx, "Alert No Network!!!!!", 0).show();
		^
Main.java:214: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
		Log.d(TAG, "Alert No Network!!!!!");
		^
Main.java:225: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
		Log.d("Network", "Checking Network... (Without Checking!!!!!)");
		^
Main.java:237: cannot find symbol
symbol  : variable Log
location: class com.cramdroid.api.HttpHandler
		Log.d("Network", "Checking Network Done!!");
		^
72 errors
stdout
Standard output is empty