1 package com.beloko.idtech;
2 
3 import java.io.BufferedInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 
8 import org.apache.http.HttpResponse;
9 import org.apache.http.client.HttpClient;
10 import org.apache.http.client.methods.HttpPost;
11 import org.apache.http.impl.client.DefaultHttpClient;
12 
13 import android.app.AlertDialog;
14 import android.app.ProgressDialog;
15 import android.content.Context;
16 import android.content.DialogInterface;
17 import android.os.AsyncTask;
18 import android.util.Log;
19 
20 public class SimpleServerAccess {
21 	String LOG = "SimpleServerAccess";
22 	Context ctx;
returnData(ByteArrayOutputStream data)23 	void returnData(ByteArrayOutputStream data)
24 	{
25 		//OVerride me
26 	}
27 
28 
SimpleServerAccess(Context context,String url)29 	SimpleServerAccess(Context context,String url)
30 	{
31 		ctx = context;
32 		new ServerAccessThread().execute(url);
33 	}
34 
35 	private class ServerAccessThread extends AsyncTask<String, Integer, Long> {
36 
37 		private ProgressDialog progressBar;
38 		String errorstring= null;
39 		ByteArrayOutputStream data_out = new ByteArrayOutputStream();
40 
41 		@Override
onPreExecute()42 		protected void onPreExecute() {
43 			progressBar = new ProgressDialog(ctx);
44 			progressBar.setMessage("Accessing Server..");
45 			progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
46 			progressBar.setCancelable(false);
47 			progressBar.show();
48 		}
49 
doInBackground(String... url)50 		protected Long doInBackground(String... url) {
51 
52 			String url_full = url[0];
53 
54 
55 			try
56 			{
57 
58 				if (GD.DEBUG) Log.d(LOG,url_full);
59 
60 				HttpClient httpclient = new DefaultHttpClient();
61 
62 
63 				HttpPost httppost = new HttpPost(url_full);
64 
65 				HttpResponse httpResponse = null;
66 				httpResponse = httpclient.execute(httppost);
67 
68 				int code = httpResponse.getStatusLine().getStatusCode();
69 
70 				if (GD.DEBUG) Log.d(LOG,"code = " + code);
71 				if (GD.DEBUG)Log.d(LOG,"reason = " + httpResponse.getStatusLine().getReasonPhrase());
72 
73 				if (code != 200)
74 				{
75 					errorstring = httpResponse.getStatusLine().getReasonPhrase();
76 					return 1L;
77 				}
78 
79 				int dlSize = (int)httpResponse.getEntity().getContentLength();
80 
81 				BufferedInputStream in = null;
82 
83 				InputStream ins = httpResponse.getEntity().getContent();
84 
85 				progressBar.setMax(dlSize);
86 				if (GD.DEBUG) Log.d(LOG,"File size = " + dlSize);
87 
88 				in = new BufferedInputStream(ins);
89 
90 				byte data[] = new byte[1024];
91 				int count;
92 				while ((count = in.read(data, 0, 1024)) != -1)
93 				{
94 					data_out.write(data, 0, count);
95 				}
96 				in.close();
97 
98 
99 			} catch (IOException e) {
100 				errorstring = e.toString();
101 				return 1l;
102 			}
103 
104 			return 0l;
105 		}
106 
onProgressUpdate(Integer... progress)107 		protected void onProgressUpdate(Integer... progress) {
108 
109 		}
110 
onPostExecute(Long result)111 		protected void onPostExecute(Long result) {
112 			progressBar.dismiss();
113 			if (errorstring!=null)
114 			{
115 				AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
116 				builder.setMessage("Error accessing server: " + errorstring)
117 				.setCancelable(true)
118 				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
119 					public void onClick(DialogInterface dialog, int id) {
120 
121 					}
122 				});
123 
124 				builder.show();
125 			}
126 			else
127 			{
128 				returnData(data_out);
129 			}
130 		}
131 	}
132 }
133