17 |
import org.apache.http.message.BasicNameValuePair; |
import org.apache.http.message.BasicNameValuePair; |
18 |
|
|
19 |
import android.app.Activity; |
import android.app.Activity; |
20 |
|
import android.app.Dialog; |
21 |
|
import android.app.ProgressDialog; |
22 |
import android.content.Intent; |
import android.content.Intent; |
23 |
import android.database.Cursor; |
import android.database.Cursor; |
24 |
|
import android.location.Criteria; |
25 |
|
import android.location.Location; |
26 |
|
import android.location.LocationListener; |
27 |
|
import android.location.LocationManager; |
28 |
import android.net.Uri; |
import android.net.Uri; |
29 |
|
import android.os.AsyncTask; |
30 |
import android.os.Bundle; |
import android.os.Bundle; |
31 |
import android.util.Log; |
import android.util.Log; |
32 |
import android.view.View; |
import android.view.View; |
36 |
import android.widget.TextView; |
import android.widget.TextView; |
37 |
import android.widget.Toast; |
import android.widget.Toast; |
38 |
|
|
39 |
public class PostActivity extends Activity { |
public class PostActivity extends Activity implements LocationListener { |
40 |
static final int IMAGE_PICK = 1000; |
static final int IMAGE_PICK = 1000; |
41 |
|
|
42 |
ImageView picture; |
ImageView picture; |
44 |
TextView title; |
TextView title; |
45 |
TextView note; |
TextView note; |
46 |
|
|
47 |
|
Location currentLocation; |
48 |
|
LocationManager locManager; |
49 |
|
|
50 |
/** Called when the activity is first created. */ |
/** Called when the activity is first created. */ |
51 |
@Override |
@Override |
52 |
public void onCreate(Bundle savedInstanceState) { |
public void onCreate(Bundle savedInstanceState) { |
70 |
submit(); |
submit(); |
71 |
} |
} |
72 |
}); |
}); |
73 |
|
|
74 |
|
locManager = (LocationManager) getSystemService(LOCATION_SERVICE); |
75 |
|
Criteria c = new Criteria(); |
76 |
|
c.setAccuracy(Criteria.ACCURACY_FINE); |
77 |
|
String provider = locManager.getBestProvider(c, true); |
78 |
|
locManager.requestLocationUpdates(provider, 10, 0.0f, this); |
79 |
|
|
80 |
} |
} |
81 |
|
|
82 |
|
|
120 |
} |
} |
121 |
|
|
122 |
void submit() { |
void submit() { |
|
HttpPost post = new HttpPost("http://app.t-hoerup.dk/PicturePosterService/PostServlet"); |
|
123 |
String titleStr = title.getText().toString(); |
String titleStr = title.getText().toString(); |
124 |
String noteStr = note.getText().toString(); |
String noteStr = note.getText().toString(); |
125 |
|
|
126 |
if (titleStr.trim().length() == 0) { |
if (titleStr.trim().length() == 0) { |
127 |
Toast.makeText(this, "Please enter a title", Toast.LENGTH_LONG).show(); |
Toast.makeText(PostActivity.this, "Please enter a title", Toast.LENGTH_LONG).show(); |
128 |
return; |
return; |
129 |
} |
} |
130 |
if (noteStr.trim().length() == 0) { |
if (noteStr.trim().length() == 0) { |
131 |
Toast.makeText(this, "Please enter a note", Toast.LENGTH_LONG).show(); |
Toast.makeText(PostActivity.this, "Please enter a note", Toast.LENGTH_LONG).show(); |
132 |
return; |
return; |
133 |
} |
} |
134 |
|
|
135 |
if (media_path == null || media_path.length() == 0) { |
if (media_path == null || media_path.length() == 0) { |
136 |
Toast.makeText(this, "Please select an picture", Toast.LENGTH_LONG).show(); |
Toast.makeText(PostActivity.this, "Please select an picture", Toast.LENGTH_LONG).show(); |
137 |
return; |
return; |
138 |
} |
} |
139 |
|
|
140 |
|
UploaderTask task = new UploaderTask(); |
141 |
|
task.execute(); |
142 |
|
} |
143 |
|
|
144 |
MultipartEntity entity = new MultipartEntity(); |
|
145 |
|
|
146 |
entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") ); |
|
147 |
|
|
148 |
try { |
@Override |
149 |
List<NameValuePair> data = new ArrayList<NameValuePair>(); |
public void onLocationChanged(Location location) { |
150 |
data.add( new BasicNameValuePair("title", titleStr) ); |
currentLocation = location; |
151 |
data.add( new BasicNameValuePair("note", noteStr) ); |
locManager.removeUpdates(this); |
152 |
String encoded = URLEncodedUtils.format(data, "UTF-8"); |
} |
153 |
entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") )); |
|
154 |
} catch (Exception e) { |
@Override |
155 |
Log.e("asdasd","asdasdasd",e); |
public void onProviderDisabled(String provider) { |
156 |
|
} |
157 |
|
|
158 |
|
@Override |
159 |
|
public void onProviderEnabled(String provider) { |
160 |
|
} |
161 |
|
|
162 |
|
@Override |
163 |
|
public void onStatusChanged(String provider, int status, Bundle extras) { |
164 |
|
} |
165 |
|
|
166 |
|
class UploaderTask extends AsyncTask<Void, Void, Boolean> { |
167 |
|
String titleStr; |
168 |
|
String noteStr; |
169 |
|
|
170 |
|
Dialog dlg; |
171 |
|
|
172 |
|
@Override |
173 |
|
protected void onPreExecute() { |
174 |
|
super.onPreExecute(); |
175 |
|
titleStr = title.getText().toString(); |
176 |
|
noteStr = note.getText().toString(); |
177 |
|
|
178 |
|
dlg = ProgressDialog.show(PostActivity.this, "PicturePoster", "Uploading data"); |
179 |
} |
} |
180 |
|
|
181 |
|
@Override |
182 |
post.setEntity(entity); |
protected Boolean doInBackground(Void... params) { |
183 |
boolean ok = false; |
HttpPost post = new HttpPost("http://192.168.10.10:8080/PicturePosterService/PostServlet"); |
184 |
HttpClient client = new DefaultHttpClient(); |
|
185 |
try { |
|
186 |
HttpResponse resp = client.execute(post); |
|
187 |
if (resp.getStatusLine().getStatusCode() == 200) { |
MultipartEntity entity = new MultipartEntity(); |
188 |
ok = true; |
|
189 |
|
entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") ); |
190 |
|
|
191 |
|
try { |
192 |
|
boolean hasLoc = currentLocation != null; |
193 |
|
List<NameValuePair> data = new ArrayList<NameValuePair>(); |
194 |
|
data.add( new BasicNameValuePair("title", titleStr) ); |
195 |
|
data.add( new BasicNameValuePair("note", noteStr) ); |
196 |
|
|
197 |
|
data.add( new BasicNameValuePair("latitude", hasLoc ? Float.toString((float)currentLocation.getLatitude()) : "-") ); |
198 |
|
data.add( new BasicNameValuePair("longitude", hasLoc ? Float.toString((float)currentLocation.getLongitude()) : "-" ) ); |
199 |
|
|
200 |
|
String encoded = URLEncodedUtils.format(data, "UTF-8"); |
201 |
|
entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") )); |
202 |
|
} catch (Exception e) { |
203 |
|
Log.e("asdasd","asdasdasd",e); |
204 |
} |
} |
205 |
|
|
|
} catch (Exception e) { |
|
|
Log.d("PostActivity", "upload failed", e); |
|
206 |
|
|
207 |
|
post.setEntity(entity); |
208 |
|
boolean ok = false; |
209 |
|
HttpClient client = new DefaultHttpClient(); |
210 |
|
try { |
211 |
|
HttpResponse resp = client.execute(post); |
212 |
|
Log.d("PostActivity", "" + resp.getStatusLine().getStatusCode() + ": " + resp.getStatusLine().getReasonPhrase() ); |
213 |
|
if (resp.getStatusLine().getStatusCode() == 200) { |
214 |
|
ok = true; |
215 |
|
} |
216 |
|
|
217 |
|
} catch (Exception e) { |
218 |
|
Log.d("PostActivity", "upload failed", e); |
219 |
|
} |
220 |
|
return ok; |
221 |
|
|
222 |
} |
} |
223 |
if (ok == true) { |
@Override |
224 |
Toast.makeText(this, "Upload succeeded", Toast.LENGTH_LONG).show(); |
protected void onPostExecute(Boolean result) { |
225 |
} else { |
super.onPostExecute(result); |
226 |
Toast.makeText(this, "Upload failed", Toast.LENGTH_LONG).show(); |
|
227 |
|
dlg.dismiss(); |
228 |
|
|
229 |
|
if (result == true) { |
230 |
|
Toast.makeText(PostActivity.this, "Upload succeeded", Toast.LENGTH_LONG).show(); |
231 |
|
} else { |
232 |
|
Toast.makeText(PostActivity.this, "Upload failed", Toast.LENGTH_LONG).show(); |
233 |
|
} |
234 |
} |
} |
235 |
} |
} |
|
|
|
236 |
} |
} |