Limo

formactivity android

Jun 20th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 KB | None | 0 0
  1. package com.mclinic.view.sample.activities;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19. import org.apache.http.message.BasicNameValuePair;
  20. import org.apache.http.params.BasicHttpParams;
  21. import org.apache.http.params.HttpConnectionParams;
  22. import org.apache.http.params.HttpParams;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25. import com.tbform.*;
  26.  
  27.  
  28. import com.mclinic.view.sample.R;
  29. import android.app.Activity;
  30. import android.app.ProgressDialog;
  31. import android.content.Context;
  32. import android.os.AsyncTask;
  33. import android.os.Bundle;
  34. import android.util.Log;
  35. import android.view.View;
  36. import android.view.inputmethod.InputMethodManager;
  37. import android.widget.EditText;
  38. import android.widget.Toast;
  39.  
  40.  
  41.  
  42.  
  43. public class Aformactivity extends Activity {
  44.  
  45. private static final String SERVICE_URL = "http://192.168.1.17/openmrs-standalone/ws/rest/v1/muzima/queueData";
  46.  
  47. private static final String TAG = "AndroidRESTClientActivity";
  48.  
  49. private static final String encounter = null;
  50.  
  51. /** Called when the activity is first created. */
  52. @Override
  53. public void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.form);
  56. }
  57.  
  58. public void retrieveSampleData(View vw) {
  59.  
  60. //String sampleURL = SERVICE_URL + "/muzima/queueata";
  61.  
  62. WebServiceTask wst = new WebServiceTask(WebServiceTask.GET_TASK, this, "GETting data...");
  63.  
  64. wst.execute(new String[] { sampleURL });
  65.  
  66. }
  67.  
  68. public void clearControls(View vw) {
  69.  
  70. EditText eddatasource = (EditText) findViewById(R.id.datasource);
  71. EditText edpersonuuid = (EditText) findViewById(R.id.personuuid);
  72. EditText edformuuid = (EditText) findViewById(R.id.formuuid);
  73. EditText edencountertypeuuid = (EditText) findViewById(R.id.encountertypeuuid);
  74. EditText edprovideruuid = (EditText) findViewById(R.id.provideruuid);
  75. EditText edlocationuuid = (EditText) findViewById(R.id.locationuuid);
  76. EditText eddatetime = (EditText) findViewById(R.id.datetime);
  77. EditText edobsuuid = (EditText) findViewById(R.id.obsuuid);
  78.  
  79.  
  80. eddatasource.setText("");
  81. edpersonuuid.setText("");
  82. edformuuid.setText("");
  83. edencountertypeuuid.setText("");
  84. edprovideruuid.setText("");
  85. edlocationuuid.setText("");
  86. eddatasource.setText("");
  87. edobsuuid.setText("");
  88.  
  89.  
  90. }
  91.  
  92. public void postData(View vw){
  93.  
  94. EditText eddatasource = (EditText) findViewById(R.id.datasource);
  95. EditText edpersonuuid = (EditText) findViewById(R.id.personuuid);
  96. EditText edformuuid = (EditText) findViewById(R.id.formuuid);
  97. EditText edencountertypeuuid = (EditText) findViewById(R.id.encountertypeuuid);
  98. EditText edprovideruuid = (EditText) findViewById(R.id.provideruuid);
  99. EditText edlocationuuid = (EditText) findViewById(R.id.locationuuid);
  100. EditText eddatetime = (EditText) findViewById(R.id.datetime);
  101. EditText edobsuuid = (EditText) findViewById(R.id.obsuuid);
  102.  
  103.  
  104. String datasource = eddatasource.getText().toString();
  105. String personuuid = edpersonuuid.getText().toString();
  106. String formuuid = edformuuid.getText().toString();
  107. String encountertypeuuid= edencountertypeuuid.getText().toString();
  108. String provideruuid = edprovideruuid.getText().toString();
  109. String locationuuid = edlocationuuid.getText().toString();
  110. String datetime = eddatetime.getText().toString();
  111. String obsuuid = edobsuuid.getText().toString();
  112.  
  113. if (datasource.equals("") || personuuid.equals("")|| formuuid.equals("") || encountertypeuuid.equals("")|| provideruuid.equals("")|| locationuuid.equals("")|| datetime.equals("")|| obsuuid.equals("")) {
  114. Toast.makeText(this, "Please enter in all required fields.",
  115. Toast.LENGTH_LONG).show();
  116. return;
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, this, "Posting data...");
  124.  
  125. /*JSONObject Datasource = new JSONObject();
  126. Datasource.put("datasource","954b9e59-eba0-4679-afbb-2878580c054d");
  127.  
  128. JSONObject Tbform = new JSONObject();
  129. Tbform.put(datasource, Datasource);
  130. Tbform.put(discriminator, encounter);
  131. Tbform.put(payload, payload);
  132.  
  133. JSONObject Payload=new JSONObject();
  134. Payload.put(person, personuuid);
  135. Payload.put(encounter,encounter);
  136. Payload.put(obs, obs);
  137.  
  138. JSONObject Encounter=new JSONObject();
  139. Encounter.put(form_uuid, formuuid);
  140. Encounter.put(encounterType_uuid,encountertypeuuid);
  141. Encounter.put(provider_uuid,provideruuid);
  142. Encounter.put(location_uuid,locationuuid);
  143. Encounter.put(datetime,datetime);
  144.  
  145.  
  146. JSONObject Person=new JSONObject();
  147. Person.put(person_uuid,personuuid);
  148.  
  149. JSONObject Ob=new JSONObject();
  150. Ob.put(uuid,obsuuid);
  151. Ob.put(type, coded) ;
  152. Ob.put(value,value);
  153.  
  154. */
  155.  
  156. wst.addNameValuePair("Datasource", datasource);
  157. wst.addNameValuePair("Enncounter", encounter);
  158. String discriminator = "encounter";
  159. wst.addNameValuePair("discriminator", discriminator);
  160. String payload = null;
  161. wst.addNameValuePair("payload", payload);
  162.  
  163.  
  164.  
  165. // the passed String is the URL we will POST to
  166. wst.execute(new String[] { SERVICE_URL });
  167.  
  168. }
  169.  
  170. public void handleResponse(String response) {
  171.  
  172. EditText eddatasource = (EditText) findViewById(R.id.datasource);
  173. EditText edprovideruuid = (EditText) findViewById(R.id.provideruuid);
  174. EditText eddatetime = (EditText) findViewById(R.id.datetime);
  175.  
  176. eddatasource.setText("");
  177. edprovideruuid.setText("");
  178. eddatetime.setText("");
  179.  
  180. try {
  181.  
  182. JSONObject jso = new JSONObject(response);
  183.  
  184. String datasource = jso.getString("datasource");
  185. String provideruuid = jso.getString("provideruuid");
  186. String datetime = jso.getString("datetime");
  187.  
  188. eddatasource.setText(datasource);
  189. edprovideruuid.setText(provideruuid);
  190. eddatetime.setText(datetime);
  191.  
  192. } catch (Exception e) {
  193. Log.e(TAG, e.getLocalizedMessage(), e);
  194. }
  195.  
  196. }
  197.  
  198. private void hideKeyboard() {
  199.  
  200. InputMethodManager inputManager = (InputMethodManager) Aformactivity.this
  201. .getSystemService(Context.INPUT_METHOD_SERVICE);
  202.  
  203. inputManager.hideSoftInputFromWindow(
  204. Aformactivity.this.getCurrentFocus()
  205. .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  206. }
  207.  
  208.  
  209. private class WebServiceTask extends AsyncTask<String, Integer, String> {
  210.  
  211. public static final int POST_TASK = 1;
  212. public static final int GET_TASK = 2;
  213.  
  214. private static final String TAG = "WebServiceTask";
  215.  
  216. // connection timeout, in milliseconds (waiting to connect)
  217. private static final int CONN_TIMEOUT = 3000;
  218.  
  219. // socket timeout, in milliseconds (waiting for data)
  220. private static final int SOCKET_TIMEOUT = 5000;
  221.  
  222. private int taskType = GET_TASK;
  223. private Context mContext = null;
  224. private String processMessage = "Processing...";
  225.  
  226. private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
  227.  
  228. private ProgressDialog pDlg = null;
  229.  
  230. public WebServiceTask(int taskType, Context mContext, String processMessage) {
  231.  
  232. this.taskType = taskType;
  233. this.mContext = mContext;
  234. this.processMessage = processMessage;
  235. }
  236.  
  237. public void addNameValuePair(String name, String value) {
  238.  
  239. params.add(new BasicNameValuePair(name, value));
  240. }
  241.  
  242. private void showProgressDialog() {
  243.  
  244. pDlg = new ProgressDialog(mContext);
  245. pDlg.setMessage(processMessage);
  246. pDlg.setProgressDrawable(mContext.getWallpaper());
  247. pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  248. pDlg.setCancelable(false);
  249. pDlg.show();
  250.  
  251. }
  252.  
  253. @Override
  254. protected void onPreExecute() {
  255.  
  256. hideKeyboard();
  257. showProgressDialog();
  258.  
  259. }
  260.  
  261. protected String doInBackground(String... urls) {
  262.  
  263. String url = urls[0];
  264. String result = "";
  265.  
  266. HttpResponse response = doResponse(url);
  267.  
  268. if (response == null) {
  269. return result;
  270. } else {
  271.  
  272. try {
  273.  
  274. result = inputStreamToString(response.getEntity().getContent());
  275.  
  276. } catch (IllegalStateException e) {
  277. Log.e(TAG, e.getLocalizedMessage(), e);
  278.  
  279. } catch (IOException e) {
  280. Log.e(TAG, e.getLocalizedMessage(), e);
  281. }
  282.  
  283. }
  284.  
  285. return result;
  286. }
  287.  
  288. @Override
  289. protected void onPostExecute(String response) {
  290.  
  291. handleResponse(response);
  292. pDlg.dismiss();
  293.  
  294. }
  295.  
  296. // Establish connection and socket (data retrieval) timeouts
  297. private HttpParams getHttpParams() {
  298.  
  299. HttpParams htpp = new BasicHttpParams();
  300.  
  301. HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
  302. HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);
  303.  
  304. return htpp;
  305. }
  306.  
  307. private HttpResponse doResponse(String url) {
  308.  
  309. // Use our connection and data timeouts as parameters for our
  310. // DefaultHttpClient
  311. HttpClient httpclient = new DefaultHttpClient(getHttpParams());
  312.  
  313. HttpResponse response = null;
  314.  
  315. try {
  316. switch (taskType) {
  317.  
  318. case POST_TASK:
  319. HttpPost httppost = new HttpPost(url);
  320. // Add parameters
  321. httppost.setEntity(new UrlEncodedFormEntity(params));
  322.  
  323. response = httpclient.execute(httppost);
  324. break;
  325. case GET_TASK:
  326. HttpGet httpget = new HttpGet(url);
  327. response = httpclient.execute(httpget);
  328. break;
  329. }
  330. } catch (Exception e) {
  331.  
  332. Log.e(TAG, e.getLocalizedMessage(), e);
  333.  
  334. }
  335.  
  336. return response;
  337. }
  338.  
  339. private String inputStreamToString(InputStream is) {
  340.  
  341. String line = "";
  342. StringBuilder total = new StringBuilder();
  343.  
  344. // Wrap a BufferedReader around the InputStream
  345. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  346.  
  347. try {
  348. // Read response until the end
  349. while ((line = rd.readLine()) != null) {
  350. total.append(line);
  351. }
  352. } catch (IOException e) {
  353. Log.e(TAG, e.getLocalizedMessage(), e);
  354. }
  355.  
  356. // Return full string
  357. return total.toString();
  358. }
  359.  
  360. }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment