Skip to content

Commit

Permalink
First go at submitting to server
Browse files Browse the repository at this point in the history
  • Loading branch information
rpassmore committed Mar 8, 2011
1 parent 99f79d2 commit 5ce8185
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
47 changes: 43 additions & 4 deletions src/rpassmore/app/fillthathole/Hazard.java
Expand Up @@ -21,7 +21,14 @@
*/
package rpassmore.app.fillthathole;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

public class Hazard {

Expand Down Expand Up @@ -265,11 +272,43 @@ public void setHazardId(String hazardId) {
*/
public String getHazardId() {
return hazardId;
}

}
public String createSubmitStr() {
String str = "{ + " + locationDesc + "}";

/*
String str = "";
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("hazardDescription", getHazardDesc());
jsonObj.put("onRailwayCrossing", isOnLevelCrossing());
jsonObj.put("longitude", getLongitude());
jsonObj.put("latitude", getLattitude());
jsonObj.put("email", "");
jsonObj.put("onTowpath", isOnTowPath());
jsonObj.put("hazardType", getHazardType() + 1);
jsonObj.put("onRedRoute", isOnRedRoute());
jsonObj.put("locationDescription", getLocationDesc());
str = jsonObj.toString(3);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/

/*Translate the class into a json esque string
* data has to be in this format or it is not accepted by the server*/
String str = "data={\"hazardDescription\":\"" + getHazardDesc()
+ "\",\"onRailwayCrossing\":" + (isOnLevelCrossing() ? 1 : 0)
+ ",\"longitude\":" + getLongitude()
+ ",\"latitude\":" + getLattitude()
+ ",\"email\":\"\""
+ ",\"onTowpath\":" + (isOnTowPath() ? 1 : 0)
+ ",\"hazardType\":" + (getHazardType() + 1)
+ ",\"onRedRoute\":" + (isOnRedRoute() ? 1 : 0)
+ ",\"locationDescription\":\"" + getLocationDesc()
+ "\"}";
//TODO add depth, dist from kerb and size
return str;
}

Expand Down
1 change: 1 addition & 0 deletions src/rpassmore/app/fillthathole/MyHazardsActivity.java
Expand Up @@ -164,6 +164,7 @@ public void onClick(DialogInterface dialog, int id) {
Hazard hazard = dbAdapter.load(info.id);
hazard.setState(Hazard.State.FIXED);
dbAdapter.save(hazard);
cursor.requery();
} catch (SQLException e) {
Log.e(getPackageName(), e.toString());
}
Expand Down
51 changes: 47 additions & 4 deletions src/rpassmore/app/fillthathole/ViewHazardActivity.java
Expand Up @@ -26,6 +26,22 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
Expand Down Expand Up @@ -177,7 +193,7 @@ public void onPause() {
/**
*
*/
private void makeActivityReadOnly() {
private void makeActivityReadOnly() {
if (hazard.getState() != Hazard.State.UNSUBMITTED) {
locationDesc.setEnabled(false);
hazardType.setEnabled(false);
Expand Down Expand Up @@ -229,7 +245,34 @@ public void submitClickHandler(View view) {
// if the hazard has been submitted successfully set the state to
// submitted
// if photo has been submitted successfully set hasPhoto true





try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://www.fillthathole.org.uk/services/submit_hazard";
HttpPost post = new HttpPost(postURL);
//hopefully dont need to set user agent
//post.setHeader("User-Agent", "Fill%20That%20Hole/1.11 CFNetwork/485.12.7 Darwin/10.4.0");
ByteArrayEntity ent = new ByteArrayEntity(hazard.createSubmitStr().getBytes("UTF8"));
ent.setContentType("application/x-www-form-urlencoded");
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
String s = EntityUtils.toString(resEntity);
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
Log.e(getPackageName(), "Error submitting hazard", e);
}

hazard.setState(Hazard.State.SUMITTED);
dbAdapter.open();
dbAdapter.save(hazard);
dbAdapter.close();
break;
}
finish();
Expand Down Expand Up @@ -291,7 +334,7 @@ private void storeNewPhoto(Bitmap bitmap, String photoUrl) {
try {
out.close();
} catch (IOException e) {
Log.e(getPackageName(), e.toString());
Log.e(getPackageName(), "Error scaling and storing thumbnail", e);
}

// display the thumbnail
Expand All @@ -315,7 +358,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
storeNewPhoto(bitmap, data.getData().toString());
} catch (FileNotFoundException ex) {
Log.e(getPackageName(), ex.toString());
Log.e(getPackageName(), "Error loading image file", ex);
}
}
break;
Expand Down

0 comments on commit 5ce8185

Please sign in to comment.