Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. String xml = "<?xml version="1.0"encoding="UTF-8"?><UserRequesting><NewUser>joseph</NewUser><Password>123456789</Password></UserRequesting>";
  2.  
  3. public void fetchLoginXML(){
  4. Log.d(TAG, "IN fetch ");
  5. HttpsURLConnection urlc;
  6. OutputStreamWriter out = null;
  7. DataOutputStream dataout;
  8. BufferedReader in = null;
  9.  
  10. try {
  11. URL url = new URL(urlValuser);
  12. Log.d(TAG, "Final URL: " + url);
  13. urlc = (HttpsURLConnection) url.openConnection();
  14. urlc.setHostnameVerifier(new AllowAllHostnameVerifier());
  15. urlc.setRequestMethod("POST");
  16.  
  17. urlc.setDoOutput(true);
  18. urlc.setDoInput(true);
  19. urlc.setUseCaches(false);
  20. urlc.setAllowUserInteraction(false);
  21. urlc.setRequestProperty("Content-Type", "text/xml");
  22. // perform POST operation
  23. Log.d(TAG, "Xml Source to POST: " + xmlsrc);
  24. String body = xmlsrc;
  25. OutputStream output = new BufferedOutputStream(urlc.getOutputStream());
  26. output.write(body.getBytes());
  27. output.flush();
  28. int responseCode = urlc.getResponseCode();
  29. in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);
  30.  
  31. String response = "";
  32.  
  33. String line = in.readLine();
  34. while (line != null) {
  35. response += line;
  36. line = in.readLine();
  37. }
  38.  
  39. Log.d(TAG, "Post results Response Code " + responseCode);
  40. Log.d(TAG, "Post results Response " + response);
  41.  
  42. in.close();
  43.  
  44. factory = XmlPullParserFactory.newInstance();
  45. XmlPullParser myparser = factory.newPullParser();
  46. Log.d(TAG, "Setting myparser paramaters ");
  47. myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
  48. Log.d(TAG, "Setting myparser input into xmldata ");
  49. myparser.setInput(new StringReader(response));
  50. Log.d(TAG, "send myparser to function parsexmlandstoreit ");
  51. parseXMLAndStoreIt(myparser);
  52.  
  53. } catch (Exception e) {
  54. Log.e(TAG, "Error Posting Data: " + e.toString());
  55. } finally {
  56. if (out != null) {
  57. try {
  58. out.close();
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. if (in != null) {
  64. try {
  65. in.close();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. }
  71. }
  72.  
  73. public void postData(String sendData) throws Exception {
  74. // Create a new HttpClient and Post Header
  75. Log.d(TAG, "Sending Data: " + sendData);
  76. HttpClient httpclient = new DefaultHttpClient();
  77. HttpPost httppost = new HttpPost("https://joes....");
  78. httppost.addHeader("Accept", "text/xml");
  79.  
  80. try {
  81. StringEntity se = new StringEntity(sendData);
  82. se.setContentType("text/xml");
  83. httppost.setEntity(se);
  84. // Execute HTTP Post Request
  85. Log.d(TAG, "Execute HTTP POST");
  86. HttpResponse response = httpclient.execute(httppost);
  87. Log.d(TAG, "Message Sent :)");
  88. InputStream ips = response.getEntity().getContent();
  89. BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
  90.  
  91. if(response.getStatusLine().getStatusCode()!= HttpStatus.SC_OK)
  92. {
  93. Log.e(TAG, "Response: " + response.getStatusLine().getReasonPhrase());
  94. throw new Exception(response.getStatusLine().getReasonPhrase());
  95. }
  96.  
  97. Log.d(TAG, "Response: " + response.getStatusLine().getReasonPhrase());
  98. String received = "";
  99.  
  100. String line = buf.readLine();
  101. while (line != null) {
  102. received += line;
  103. line = buf.readLine();
  104. }
  105.  
  106. StringBuilder sb = new StringBuilder();
  107. String s;
  108. while(true)
  109. {
  110. s = buf.readLine();
  111. if(s==null || s.length()==0)
  112. break;
  113. sb.append(s);
  114.  
  115. }
  116. buf.close();
  117. ips.close();
  118. sb.toString();
  119.  
  120. factory = XmlPullParserFactory.newInstance();
  121. XmlPullParser myparser = factory.newPullParser();
  122. Log.d(TAG, "Setting myparser paramaters ");
  123. myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
  124. Log.d(TAG, "Setting myparser input into xmldata ");
  125. myparser.setInput(new StringReader(received));
  126. Log.d(TAG, "send myparser to function parsexmlandstoreit ");
  127. parseXMLAndStoreIt(myparser);
  128. }
  129. catch (ClientProtocolException e)
  130. {
  131. Log.d(TAG, "Client Protocol Error: " + e);
  132. e.printStackTrace();
  133. // TODO Auto-generated catch block
  134. }
  135. catch (IOException e)
  136. {
  137. Log.d(TAG, "I/O Error: " + e);
  138. e.printStackTrace();
  139. // TODO Auto-generated catch block
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement