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 72
|
public class MainActivity extends Activity {
private String CLASS_TAG = "BenTests";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//String adress = "http://postcatcher.in/catchers/xxxxxxxxxxxx";
String adress = "http://localhost/HealthDataServer/jsv/reply/Gateway_Acknowledgement";
String message = "<Gateway_Acknowledgement xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/ServiceStack.Acknowledgement\"><Id>0</Id><acknowledgementFilename>test</acknowledgementFilename><acknowledgementSequenceid>test</acknowledgementSequenceid><gatewayinfoLocalid>test</gatewayinfoLocalid><gatewayinfoSessionid>test</gatewayinfoSessionid><gatewayinfoSoftwareversion>test</gatewayinfoSoftwareversion><gatewayinfoTimestamp>test</gatewayinfoTimestamp></Gateway_Acknowledgement>";
new MyAsyncTask().execute(adress, message);
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0], params[1]);
return null;
}
protected void onPostExecute(Double result){
//pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Command sent!", Toast.LENGTH_LONG).show();
}
public void postData(String adress, String message) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(adress);
try {
// Add your data
httppost.setEntity(new StringEntity(message.toString()));
// Add Headers
/* For Test
//httppost.setHeader("Accept", "application/x-www-form-urlencoded; encoding='utf-8'");
//httppost.setHeader("Content-type", "application/x-www-form-urlencoded; encoding='utf-8'");
*/
// To send XML file
httppost.setHeader("Accept", "application/xml;");
httppost.setHeader("Content-type", "application/xml;");
httppost.setHeader("Accept", "length");
httppost.setHeader("Content-Length", "length");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String status = response.getStatusLine().toString();
// Get response messages
Log.d(CLASS_TAG + "showOnScreenMessage", "Message sent! ");
Log.d(CLASS_TAG + "showOnScreenMessage", "Status : " + status );
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//Toast.makeText(getApplicationContext(), "Message not sent!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
} |
Partager