Created
September 21, 2012 03:54
-
-
Save ChrisRisner/3759636 to your computer and use it in GitHub Desktop.
geodemo-android-3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class GetSASTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
getSas(); | |
return _blobImagePostString; | |
} | |
/*** | |
* Handles results of getting SAS. | |
* This happens on the UI Thread | |
*/ | |
protected void onPostExecute(String result) { | |
mLblSASDetails.setText(result); | |
mProgressDialog.dismiss(); | |
mBtnGetSAS.setEnabled(false); | |
mBtnSavePOI.setEnabled(true); | |
} | |
} | |
//Fetches a SAS URL from the server | |
protected void getSas() { | |
try { | |
String fetchUrl = String.format(Constants.kBlobSASUrl, Constants.kContainerName, | |
System.currentTimeMillis()); | |
URL url = new URL(fetchUrl); | |
HttpURLConnection urlConnection = (HttpURLConnection) url | |
.openConnection(); | |
InputStream in = new BufferedInputStream( | |
urlConnection.getInputStream()); | |
BufferedReader r = new BufferedReader(new InputStreamReader(in)); | |
StringBuilder total = new StringBuilder(); | |
String line; | |
while ((line = r.readLine()) != null) { | |
total.append(line); | |
} | |
_blobImagePostString = total.toString(); | |
} catch (Exception ex) { | |
Log.e("AddPOI", "Error:" + ex.getMessage()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class PostPointOfInterestTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
return postPointOfInterestToServer(); | |
} | |
/*** | |
* Handles results of trying to post a POI. | |
* This occurs on the UI thread | |
*/ | |
@Override | |
protected void onPostExecute(String result) { | |
if (result.equals("Created")) { | |
setResult(4321); | |
finish(); | |
} else if (result.equals("FAIL-IMAGE")) | |
Toast.makeText( | |
getApplicationContext(), | |
"You must select an image from the gallery prior to uploading a POI.", | |
Toast.LENGTH_LONG).show(); | |
else if (result.equals("FAIL-LOCATION")) | |
Toast.makeText(getApplicationContext(), | |
"You must set a location prior to uploading a POI.", | |
Toast.LENGTH_LONG).show(); | |
else | |
Toast.makeText(mActivity, "POI upload FAILED", | |
Toast.LENGTH_SHORT).show(); | |
mProgressDialog.dismiss(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String postPointOfInterestToServer() { | |
try { | |
// Make sure we have an image selected | |
if (this.mImageUrl == null) { | |
return "FAIL-IMAGE"; | |
} | |
// Make sure we have a location | |
if (mCurrentLocation == null) { | |
return "FAIL-LOCATION"; | |
} | |
Cursor cursor = getContentResolver().query(this.mImageUrl, null,null, null, null); | |
cursor.moveToFirst(); | |
int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); | |
String absoluteFilePath = cursor.getString(index); | |
FileInputStream fis = new FileInputStream(absoluteFilePath); | |
int bytesRead = 0; | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
byte[] b = new byte[1024]; | |
while ((bytesRead = fis.read(b)) != -1) { | |
bos.write(b, 0, bytesRead); | |
} | |
byte[] bytes = bos.toByteArray(); | |
// Post our byte array to the server | |
URL url = new URL(_blobImagePostString.replace("\"", "")); | |
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setDoOutput(true); | |
urlConnection.setRequestMethod("PUT"); | |
urlConnection.addRequestProperty("Content-Type", "image/jpeg"); | |
urlConnection.setRequestProperty("Content-Length", ""+ bytes.length); | |
// Write image data to server | |
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream()); | |
wr.write(bytes); | |
wr.flush(); | |
wr.close(); | |
int response = urlConnection.getResponseCode(); | |
if (response == 201 | |
&& urlConnection.getResponseMessage().equals("Created")) { | |
// We've posted succesfully, let's build the JSON data | |
JSONObject jsonUrl = new JSONObject(); | |
try { | |
jsonUrl.put("Description", absoluteFilePath.substring(absoluteFilePath.lastIndexOf("/") + 1)); | |
UUID uuid = UUID.randomUUID(); | |
jsonUrl.put("Id", uuid.toString()); | |
jsonUrl.put("Latitude", mCurrentLocation.getLatitude()); | |
jsonUrl.put("Longitude", mCurrentLocation.getLongitude()); | |
jsonUrl.put("Type", 1); | |
jsonUrl.put("Url",this._blobImagePostString.split("\\?")[0]); | |
} catch (JSONException e) { | |
Log.e("AddPOI", "Exception building JSON: " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
HttpURLConnection newPOIUrlConnection = null; | |
URL newPOIUrl = new URL(Constants.kAddPOIUrl); | |
newPOIUrlConnection = (HttpURLConnection) newPOIUrl.openConnection(); | |
newPOIUrlConnection.setDoOutput(true); | |
newPOIUrlConnection.setRequestMethod("POST"); | |
newPOIUrlConnection.addRequestProperty("Content-Type","application/json"); | |
newPOIUrlConnection.setRequestProperty("Content-Length",""+ Integer.toString(jsonUrl.toString().getBytes().length)); | |
// Write json data to server | |
DataOutputStream newPoiWR = new DataOutputStream(newPOIUrlConnection.getOutputStream()); | |
newPoiWR.writeBytes(jsonUrl.toString()); | |
newPoiWR.flush(); | |
newPoiWR.close(); | |
int newPoiResponse = urlConnection.getResponseCode(); | |
return newPOIUrlConnection.getResponseMessage(); | |
} | |
// End of post of byte array to server | |
} catch (Exception ex) { | |
Log.e("AddPOI", "Error in image upload: " + ex.getMessage()); | |
} | |
return "BIGFAIL"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment