1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
public class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0], params[1], params[2]);
return null;
}
protected void onPostExecute(Double result){
//pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Command sent!", Toast.LENGTH_SHORT).show();
}
public void postData(String address, String message, String id_to_update) {
int ID_to_update = Integer.parseInt(id_to_update);
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(address);
try {
// Add your data
httppost.setEntity(new StringEntity(message.toString()));
// Execute HTTP Post Request, send message
try{
HttpResponse response = httpclient.execute(httppost);
// Get the response : if response == 201, the message has be received, Set True to acknowledgment_message
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED){
Log.d(CLASS_TAG + "showOnScreenMessage", "Got to Server! ");
// Update acknowledgment_message to true
if(address == "http://192.168.1.147/HealthDataServer/json/reply/LifeTouch_PeakData"){
MeasurementStorage.updateHeartBeatMeasurement(ID_to_update, true);
PendingMessagesCounter.Counter(-1);
}
else if(address == "http://192.168.1.147/HealthDataServer/json/reply/LifeTouch_HeartRate"){
MeasurementStorage.updateHeartRateMeasurement(ID_to_update, true);
PendingMessagesCounter.Counter(-1);
sendMessageStatus(true);
}
else if(address == "http://192.168.1.147/HealthDataServer/json/reply/Oximeter_RawDataDataPoint"){
MeasurementStorage.updateOximeterMeasurement(ID_to_update, true);
PendingMessagesCounter.Counter(-1);
}
else if(address == "http://192.168.1.147/HealthDataServer/json/reply/LifeTouch_RespRate"){
MeasurementStorage.updateRespirationMeasurement(ID_to_update, true);
PendingMessagesCounter.Counter(-1);
}
else{
Log.d(CLASS_TAG + "Error Update", "Bad address");
}
}
}
catch(Exception ex){
Log.d(CLASS_TAG + "showOnScreenMessage", "Server not responding - or server rejecting! ");
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d(CLASS_TAG + "Error Sending data", ""+e);
}
}
} |
Partager