Android 从web服务获取响应时出错
我创建了一个Android应用程序,它必须连接到一个web服务(由我创建),该服务存储在一个数据库中(托管在一个免费托管的网站上)。web服务需要Android 从web服务获取响应时出错,android,web-services,ksoap2,Android,Web Services,Ksoap2,我创建了一个Android应用程序,它必须连接到一个web服务(由我创建),该服务存储在一个数据库中(托管在一个免费托管的网站上)。web服务需要纬度、经度和文本存储在数据库中,如果我使用它,效果会非常好 但我无法使用Android应用程序中的web服务。 代码如下: package com.example.mobile; import java.io.IOException; import org.ksoap2.SoapEnvelope; import org.ksoap2.seriali
纬度
、经度
和文本
存储在数据库中,如果我使用它,效果会非常好
但我无法使用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);
}
}
调用SOAP方法时可能出现问题,但我不知道如何修复它。您的代码似乎正确地调用了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>
插入成功
底线是:您的客户机或web服务必须正确退出“
我的意见?这应该在web服务中完成,而不是在客户机中,因为取消清除转义会使您的服务容易受到攻击。另外,
PO'
本身不是无效的字符串,您的服务不应为此生成错误。尝试此SoapObjectresult=(SoapObject)envelope.bodyIn;。另一个需要注意的问题是您正在doInbackground()中更新ui。在onPostExecute()中执行如果我使用SoapObject,它会给我一个由ClassCastException引起的“RunTimeException”,这就是我使用SoapPrimitive的原因。我认为问题在这里:requestTest.addProperty(“latitudine”,latitudine);addProperty(“longitudine”,longitudine);addProperty(“testo”,ocr);以这种方式使用addProperty向web服务发送3个不同的值是否正确?使用有效值(例如“Powe”),我从logcat:SoapFault-faultcode:'soap:Server'faultstring:'服务器无法处理请求。-->列计数与第1行“faultactor:“null”详细信息:org.kxml2.kdom处的值计数不匹配。Node@4197e128That这不是web服务的问题。在数据库上插入(或选择)的业务方法没有正确处理其参数。使用这些参数调用业务方法将产生错误。web服务正在转发该错误。在方法中,谁构建发送到数据库的SQL字符串。它发送了错误的列数。例如,表,比如说MYTABLE
,有3列,它正在向MYTABLE发送类似INSERT的内容(“value1”、“value2”)
。看到了吗MYTABLE
有三列,它只发送两个值。可能是因为发送到该方法的参数之一是null
。我试图在logcat上标记纬度、经度和ocr的值,这是我需要发送的三个值,它们不是null。但是我在联系网络服务时一直遇到这个问题。。
<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>
<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>