Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String xml = "<?xml version="1.0"encoding="UTF-8"?><UserRequesting><NewUser>joseph</NewUser><Password>123456789</Password></UserRequesting>";
- public void fetchLoginXML(){
- Log.d(TAG, "IN fetch ");
- HttpsURLConnection urlc;
- OutputStreamWriter out = null;
- DataOutputStream dataout;
- BufferedReader in = null;
- try {
- URL url = new URL(urlValuser);
- Log.d(TAG, "Final URL: " + url);
- urlc = (HttpsURLConnection) url.openConnection();
- urlc.setHostnameVerifier(new AllowAllHostnameVerifier());
- urlc.setRequestMethod("POST");
- urlc.setDoOutput(true);
- urlc.setDoInput(true);
- urlc.setUseCaches(false);
- urlc.setAllowUserInteraction(false);
- urlc.setRequestProperty("Content-Type", "text/xml");
- // perform POST operation
- Log.d(TAG, "Xml Source to POST: " + xmlsrc);
- String body = xmlsrc;
- OutputStream output = new BufferedOutputStream(urlc.getOutputStream());
- output.write(body.getBytes());
- output.flush();
- int responseCode = urlc.getResponseCode();
- in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);
- String response = "";
- String line = in.readLine();
- while (line != null) {
- response += line;
- line = in.readLine();
- }
- Log.d(TAG, "Post results Response Code " + responseCode);
- Log.d(TAG, "Post results Response " + response);
- in.close();
- factory = XmlPullParserFactory.newInstance();
- XmlPullParser myparser = factory.newPullParser();
- Log.d(TAG, "Setting myparser paramaters ");
- myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
- Log.d(TAG, "Setting myparser input into xmldata ");
- myparser.setInput(new StringReader(response));
- Log.d(TAG, "send myparser to function parsexmlandstoreit ");
- parseXMLAndStoreIt(myparser);
- } catch (Exception e) {
- Log.e(TAG, "Error Posting Data: " + e.toString());
- } finally {
- if (out != null) {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public void postData(String sendData) throws Exception {
- // Create a new HttpClient and Post Header
- Log.d(TAG, "Sending Data: " + sendData);
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("https://joes....");
- httppost.addHeader("Accept", "text/xml");
- try {
- StringEntity se = new StringEntity(sendData);
- se.setContentType("text/xml");
- httppost.setEntity(se);
- // Execute HTTP Post Request
- Log.d(TAG, "Execute HTTP POST");
- HttpResponse response = httpclient.execute(httppost);
- Log.d(TAG, "Message Sent :)");
- InputStream ips = response.getEntity().getContent();
- BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
- if(response.getStatusLine().getStatusCode()!= HttpStatus.SC_OK)
- {
- Log.e(TAG, "Response: " + response.getStatusLine().getReasonPhrase());
- throw new Exception(response.getStatusLine().getReasonPhrase());
- }
- Log.d(TAG, "Response: " + response.getStatusLine().getReasonPhrase());
- String received = "";
- String line = buf.readLine();
- while (line != null) {
- received += line;
- line = buf.readLine();
- }
- StringBuilder sb = new StringBuilder();
- String s;
- while(true)
- {
- s = buf.readLine();
- if(s==null || s.length()==0)
- break;
- sb.append(s);
- }
- buf.close();
- ips.close();
- sb.toString();
- factory = XmlPullParserFactory.newInstance();
- XmlPullParser myparser = factory.newPullParser();
- Log.d(TAG, "Setting myparser paramaters ");
- myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
- Log.d(TAG, "Setting myparser input into xmldata ");
- myparser.setInput(new StringReader(received));
- Log.d(TAG, "send myparser to function parsexmlandstoreit ");
- parseXMLAndStoreIt(myparser);
- }
- catch (ClientProtocolException e)
- {
- Log.d(TAG, "Client Protocol Error: " + e);
- e.printStackTrace();
- // TODO Auto-generated catch block
- }
- catch (IOException e)
- {
- Log.d(TAG, "I/O Error: " + e);
- e.printStackTrace();
- // TODO Auto-generated catch block
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement