2013-05-02 85 views
0

我創建了一個Android應用程序,該應用程序必須連接到存儲在數據庫(託管在免費託管站點中)的Web服務(由我創建)。 Web服務需要latitude,longitudetext存儲在數據庫中,如果我使用它,它的工作效果會很好。從Web服務獲取響應時出錯

但我無法使用我的Android應用程序中的Web服務。 下面是代碼:

package com.example.mobile; 

import java.io.IOException; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import org.xmlpull.v1.XmlPullParserException; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.os.AsyncTask; 


public class WebServiceActivity extends Activity implements OnClickListener { 

    public final static String SOAP_ACTION1 = "http://ocrwebservice.somee.com/InsertIntoDB"; 
    public final static String SOAP_ACTION2 = "http://ocrwebservice.somee.com/Testing"; 
    public final static String NAMESPACE = "http://ocrwebservice.somee.com/"; 
    public final static String METHOD_NAME1 = "InsertIntoDB"; 
    public final static String METHOD_NAME2 = "Testing"; 
    public final static String URL = "http://ocrwebservice.somee.com/Service1.asmx?WSDL"; 

    Bundle extras; 
    TextView tvResult; 
    Button btnMenu; 
    String ocr; 
    int test_or_train; //valore sentinella 
    String gps, latitude, longitude; 
    double latitudine, longitudine; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_web_service); 

     tvResult = (TextView)findViewById(R.id.textViewResult); 
     btnMenu = (Button)findViewById(R.id.buttonGoToMenu); 

     extras = getIntent().getExtras(); 
     ocr = extras.getString(RecognitionActivity.OCRTEXT); 
     gps = extras.getString(RecognitionActivity.GPS); 
     test_or_train = extras.getInt(RecognitionActivity.TEST_OR_TRAIN); 

     String [] coordinate = gps.split("#"); 
     latitude = coordinate[0]; 
     longitude = coordinate[1]; 

     Log.d("VALORI", latitude); 
     Log.d("VALORI", longitude); 

     //latitudine = Double.parseDouble(latitude); 
     //longitudine = Double.parseDouble(longitude); 

     System.out.println(latitudine); 
     System.out.println(longitudine); 

     AsyncCallWS task = new AsyncCallWS(); 
     task.execute(); 

    } 

    private class AsyncCallWS extends AsyncTask<Void,Void,Void>{ 
     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      perform(); 
      return null; 
     } 
    } 

    void perform(){ 
     switch(test_or_train){ 
     case 0: //inserimento dei valori -- fase di training 
      SoapObject requestInsert = new SoapObject(NAMESPACE, METHOD_NAME1);//inizializzazione richiesta SOAP e aggiunta parametri 

      //aggiunta dei parametri per la richiesta SOAP  
      requestInsert.addProperty("latitudine", latitude); 
      requestInsert.addProperty("longitudine", longitude); 
      requestInsert.addProperty("testo", ocr); 

      //dichiarazione della versione SOAP utilizzata 
      SoapSerializationEnvelope insertEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      insertEnvelope.setOutputSoapObject(requestInsert); 
      insertEnvelope.dotNet = true; 


      try { 
       HttpTransportSE insertTransport = new HttpTransportSE(URL); 

       //chiamata del web service 
       insertTransport.call(SOAP_ACTION1, insertEnvelope); 

       SoapPrimitive insertResult = (SoapPrimitive)insertEnvelope.getResponse(); 

       if(insertResult != null) 
        tvResult.setText(insertResult.toString()); 
       } catch (IOException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (XmlPullParserException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      break; 

     case 1: 
      SoapObject requestTest = new SoapObject(NAMESPACE, METHOD_NAME2); 

      requestTest.addProperty("latitudine", latitudine); 
      requestTest.addProperty("longitudine", longitudine); 
      requestTest.addProperty("testo", ocr); 

      SoapSerializationEnvelope testEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      testEnvelope.setOutputSoapObject(requestTest); 
      testEnvelope.dotNet = true; 

      try{ 
       HttpTransportSE testTransport = new HttpTransportSE(URL); 

       testTransport.call(SOAP_ACTION2, testEnvelope); 

       SoapObject testResult = (SoapObject)testEnvelope.bodyIn; 

       if(testResult != null){ 
         if((testResult.getProperty(0).toString()).compareTo("EXISTS") == 0) 
         tvResult.setText("Il cartello fotografato è presente nel DB"); 
        else 
         tvResult.setText("Attenzione! Il cartello fotografato non è presente nel DB"); 
        } 

       }catch (IOException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (XmlPullParserException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      break; 

      } 
     btnMenu.setOnClickListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_web_service, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     Intent intentReset = new Intent(this,MainActivity.class); 
     startActivity(intentReset); 
    } 
} 

,這是日誌的貓,我得到:

05-02 15:44:46.530: W/System.err(4670): SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Po '')' at line 1' faultactor: 'null' detail: [email protected] 
05-02 15:44:46.530: W/System.err(4670):  at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:141) 
05-02 15:44:46.530: W/System.err(4670):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140) 
05-02 15:44:46.540: W/System.err(4670):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:116) 
05-02 15:44:46.540: W/System.err(4670):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:259) 
05-02 15:44:46.540: W/System.err(4670):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114) 

也許有一個問題,而我稱之爲SOAP的方法,但我不知道該怎麼修理它。

+0

試試這個SoapObjectresult =(SoapObject)envelope.bodyIn ;.另一個需要注意的問題是你正在更新doInbackground()中的ui。在onPostExecute()中執行它 – Raghunandan 2013-05-02 14:00:43

+0

它給了我一個由ClassCastException導致的「RunTimeException」,如果我使用SoapObject,那就是爲什麼我使用SoapPrimitive .. – 2013-05-02 14:28:30

+0

我認爲問題出在這裏: requestTest.addProperty(「latitudine」,latitudine) ; requestTest.addProperty(「縱向」,縱向); requestTest.addProperty(「testo」,ocr); 是否有權使用addProperty這種方式向Web服務發送3個不同的值? – 2013-05-02 14:50:11

回答

0

您的代碼似乎正確調用SOAP方法。問題在於Web服務本身。

試試這個信封:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ocr="http://ocrwebservice.somee.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ocr:InsertIntoDB> 
     <ocr:latitudine>33</ocr:latitudine> 
     <ocr:longitudine>44</ocr:longitudine> 
     <ocr:testo>Po '</ocr:testo> 
     </ocr:InsertIntoDB> 
    </soapenv:Body> 
</soapenv:Envelope> 

它將yeld你指出了同樣的錯誤:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <soap:Fault> 
     <faultcode>soap:Server</faultcode> 
     <faultstring>Server was unable to process request. ---> You have an 
     error in your SQL syntax; check the manual that corresponds to your 
     MySQL server version for the right syntax to use near ''Po '')' at 
     line 1</faultstring> 
     <detail/> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 

在另一方面,當你把'出來:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:ocr="http://ocrwebservice.somee.com/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ocr:InsertIntoDB> 
     <ocr:latitudine>33</ocr:latitudine> 
     <ocr:longitudine>44</ocr:longitudine> 
     <ocr:testo>Po </ocr:testo> 
     </ocr:InsertIntoDB> 
    </soapenv:Body> 
</soapenv:Envelope> 

插入是成功的。

底線是:無論您的客戶端還是您的Web服務都必須正確地轉義'

我的看法?這應該在Web服務中完成,而不是在客戶端中完成,因爲不正確的轉義會使您的服務容易受到SQL Injection的影響。另外PO '本身不是無效的String,您的服務不應該爲此產生錯誤。

+0

與有效值(例如「Powe」),我從logcat得到這個錯誤: SoapFault - faultcode:'肥皂:服務器'faultstring:'服務器無法處理請求。 --->列計數與第1行的值計數不匹配faultactor:'null'詳細信息:[email protected] – 2013-05-03 15:58:43

+0

這是**不是Web服務**的問題。您在數據庫中插入(或選擇)的業務方法不能正確處理它的參數。使用這些參數調用業務方法會產生錯誤。 Web服務只是轉發該錯誤。 – acdcjunior 2013-05-03 16:40:34

+0

那麼應該在哪裏出錯? – 2013-05-03 16:45:10

相關問題