2012-09-24 15 views
6

Ho il seguente codice di app per Android qui sotto. Sto cercando di connettermi a un servizio web via HTTP. Il servizio web utilizza l'asse apache. Tuttavia sto correndo nell'errore "Errore nella lettura di XMLStreamReader" nella risposta. Sono davvero bloccato e non sono sicuro di cosa posso fare. Potrebbe essere che ci sono diverse versioni di client HTTP e SOAP utilizzati sul lato server e client ?? Qualsiasi aiuto su questo sarebbe molto apprezzato. Il servizio web è molto semplice: il metodo sayHello visualizza l'argomento dato in arg0 = some_stringErrore risposta busta SOAP: errore durante la lettura di XMLStreamReader

public class MainActivity extends Activity { 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    BufferedReader in = null; 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(
       "http://10.0.0.63:8080/archibus/cxf/HelloWorld/sayHello"); 
     request.addHeader("Content-Type", "text/xml"); 
     List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("arg0", "testing")); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 
     request.setEntity(formEntity); 

     HttpResponse response = client.execute(request); 

     in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 

     String page = sb.toString(); 
     // Log.i(tag, page); 
     System.out.println(page); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
} 
+0

http://www.lalit3686.blogspot.in/2012/06/calling-soap-webservice-using- httppost.html –

risposta

7

La tua richiesta WebService non è costruito correttamente. Stai effettivamente creando una richiesta di modulo e non una richiesta SOAP effettiva.

una richiesta SOAP è un documento XML che ha un involucro e un corpo, vedere l'esempio qui SOAP Message Example on Wikipedia.

Quello che stai facendo in questo caso è una chiamata HTTP standard che emula un modulo di invio e non una chiamata SOAP.

Avete due soluzioni qui:

1- È possibile emulare il comportamento di un client SOAP creando manualmente il documento XML e della sua presentazione. Oltre l'impostazione del documento XML corretto come richiesta corpo non dimenticare di impostare le intestazioni corrette: SOAPAction, Content-Type e Content-Length

 RequestEntity requestEntity = new StringRequestEntity("<?xml version=\"1.0\"?><soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header></soap:Header><soap:Body><m:GetStockPrice xmlns:m=\"http://www.example.org/stock\"><m:StockName>IBM</m:StockName></m:GetStockPrice></soap:Body></soap:Envelope>"); 
    post.setRequestEntity(requestEntity); 

Inoltre, non dimenticare di modificare lo spazio dei nomi (m) al di sopra con lo spazio dei nomi corretto utilizzato dal tuo webservice. e il nome dell'operazione (GetStockPrice) con l'operazione che si sta tentando di richiamare. Inoltre, non dimenticare i nomi e i tipi dei parametri.

2- È possibile utilizzare Apache Axis per generare un client e l'uso che del cliente con l'applicazione

Vedere questa discussione per ulteriori informazioni e un client consigliato How to call a SOAP web service on Android

1

Codice di esempio che utilizza K-SOAP per Android.

private void sendSOAPmsg(DamageAssessmentFormPojo pojo) throws IOException, XmlPullParserException, SoapFault { 
     SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_REPORT); 
     request.addProperty("xmlBytes", Util.getSoapBase64String(pojo)); 
     request.addProperty("fileName", IO.DefaultReportName); 
     request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this)); 

     SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_REPORT); 

     if (response.toString().equalsIgnoreCase("true")) { 
      Logger.logInfo("REPORT SENT SUCCESSFULLY", "WEB-SERVICE"); 
      pojo.setReportSent(true); 
      IO.writeObject(pojo.getReportsFolderPath() + IO.DefaultReportName, pojo); 
     } else { 
      Logger.logInfo("REPORT SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString()); 
     } 
    } 

    private SoapPrimitive sendSOAPEnvelope(SoapObject request, String soapAction) throws IOException, XmlPullParserException, SoapFault { 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(WEBSERVICE.URL); 

     androidHttpTransport.call(soapAction, envelope); 
     SoapPrimitive response = (SoapPrimitive) envelope.getResponse(); 

     return response; 

    } 

    private boolean sendSOAPimage(String strImg, File pFile) throws IOException, XmlPullParserException { 
     boolean result = false; 

     if (strImg != null) { 
      SoapObject request = new SoapObject(WEBSERVICE.NAMESPACE, WEBSERVICE.METHOD_NAME_SUBMIT_IMAGE); 
      request.addProperty("imageBytes", strImg); 
      request.addProperty("fileName", pFile.getName()); 
      request.addProperty("deviceId", AppConstants.IMEI != null ? AppConstants.IMEI : Util.getIMEI(this)); 

      SoapPrimitive response = sendSOAPEnvelope(request, WEBSERVICE.SOAP_ACTION_SUBMIT_MAGE); 

      if (response.toString().equalsIgnoreCase("true")) { 

       result = true; 

       Logger.logInfo("IMAGE SENT SUCCESSFULLY", "WEB-SERVICE"); 

      } else { 
       Logger.logInfo("IMAGE SENT FAILED", "WEB-SERVICE - RESONSE " + response.toString()); 
      } 

     } 

     return result; 
    } 

// -------- Util Helper Metodo

public static String getSoapBase64String(DamageAssessmentFormPojo pojo) { 


     XmlSerializer xmlSerializer = Xml.newSerializer(); 
     StringWriter writer = new StringWriter(); 

     try { 
      xmlSerializer.setOutput(writer); 
      xmlSerializer.startDocument("UTF-8", true); 

      xmlSerializer.startTag("", XMLTags.TAG_ROD); 
      xmlSerializer.startTag("", XMLTags.TAG_ORDER); 

      xmlSerializer.startTag("", XMLTags.TAG_SEVERITY); 
      xmlSerializer.text(pojo.getCheckedSeverity_Complexity()); 
      xmlSerializer.endTag("", XMLTags.TAG_SEVERITY); 

      xmlSerializer.startTag("", XMLTags.TAG_DAMAGE_TYPE); 
      StringBuilder builder = new StringBuilder(); 
      for (String str : pojo.getCheckedDamageTypes()) { 

       builder.append(str + " , "); 

      } 
      xmlSerializer.text(builder.toString()); 
      xmlSerializer.endTag("", XMLTags.TAG_DAMAGE_TYPE); 

      xmlSerializer.startTag("", XMLTags.TAG_ENV_IMPACT); 
      xmlSerializer.text(pojo.getCheckedEnvImpact()); 
      xmlSerializer.endTag("", XMLTags.TAG_ENV_IMPACT); 

      xmlSerializer.startTag("", XMLTags.TAG_ENV_COMMENT); 
      xmlSerializer.text(pojo.getEnvImpactComments()); 
      xmlSerializer.endTag("", XMLTags.TAG_ENV_COMMENT); 

      xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_CONDITION); 
      xmlSerializer.text(pojo.getCheckedTravelCond()); 
      xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_CONDITION); 

      xmlSerializer.startTag("", XMLTags.TAG_TRAVEL_COMMENT); 
      xmlSerializer.text(pojo.getTravCondComments()); 
      xmlSerializer.endTag("", XMLTags.TAG_TRAVEL_COMMENT); 

      xmlSerializer.startTag("", XMLTags.TAG_ORDER_DATE); 
      xmlSerializer.text(pojo.getDateTime()); 
      xmlSerializer.endTag("", XMLTags.TAG_ORDER_DATE); 

      xmlSerializer.endTag("", "Order"); 
      xmlSerializer.endTag("", "ROD"); 

      xmlSerializer.endDocument(); 

     } catch (IllegalArgumentException e) { 
      Logger.logException(e); 
     } catch (IllegalStateException e) { 
      Logger.logException(e); 
     } catch (IOException e) { 
      Logger.logException(e); 
     } 

     return Base64.encode(writer.toString().getBytes()); 
    } 
Problemi correlati