Android, submit e ricevere XML tramite il metodo POST HTTP
C'è una domanda pertinente, ma non ho potuto get la risposta chiaramente.
Vorrei POST un breve codice xml
<aaaLogin inName="admin" inPassword="admin123"/>
a un indirizzo URL specifico su HTTP. Il servizio Web mi invierà un codice XML. La parte importnte è che analizzerò l'XML ricevuto e voglio memorizzare come file.
// Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.168.192.131/"); //URL address StringEntity se = new StringEntity("<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>",HTTP.UTF_8); //XML as a string se.setContentType("text/xml"); //declare it as XML httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8"); httppost.setEntity(se); BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient .execute(httppost); tvData.setText(httpResponse.getStatusLine().toString()); //text view is expected to print the response
c'è qualcosa che non va a ricevere la risposta. Inoltre, non ho scritto nulla per salvare l'XML ricevuto come file. Può qualcuno scrivere un frammento di codice?
3 Solutions collect form web for “Android, submit e ricevere XML tramite il metodo POST HTTP”
Ok, ho capito subito dopo che ho postato questa domanda. Questo codice funziona bene:
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.168.192.131/"); try { StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8); se.setContentType("text/xml"); httppost.setEntity(se); HttpResponse httpresponse = httpclient.execute(httppost); HttpEntity resEntity = httpresponse.getEntity(); tvData.setText(EntityUtils.toString(resEntity)); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
È ansible get il contenuto della risposta utilizzando:
String responseXml = EntityUtils.toString(httpResponse.getEntity());
Puoi quindi scrivere a un file utilizzando qualcosa di simile .
c'è qualcosa che non va a ricevere la risposta
Dal momento che non hai detto che cosa è sbagliato nel ricevere la risposta, è un po 'difficile aiutarti con questo punto.
Perché non utilizzare Primavera SpringTime in spring per Android ?