information found on the Internet to connect remote database to use Android WebService, with koap2 access WebService data.
I'm here to do a simple test landing:
I use Eclipse built a Dynamic Web Project Project: webTest, write the relevant code, database connectivity test was successful. Class after IPersonImp Right-click on the file name, select "Web Services" -> "Create Web Service" automatically create webservice:
The project deployed to Tomcat, start Tomcat, enter the URL in the browser, it is possible to find wsdl:
Browser Test landing effect (password is wrong, the result is fail):
problem :
create an Android project, MyWebServiceHelper class code:
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
public class MyWebServiceHelper {
// WSDL文档中的命名空间
private static final String targetNameSpace = "http://service.main";
// WSDL文档中的URL
private static final String WSDL = "http://localhost:8181/webTest/services/IPersonImp";
//需要调用的方法名(获得Myervices中的login方法)
private static final String login="login";
public String login(String uname, String pwd){
String reutrnResult = null;
SoapObject request =new SoapObject(targetNameSpace,login);
request.addProperty("uname", uname);
request.addProperty("pwd", pwd);
Log.i("mylog", "request---" + request);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = false;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTranstation = new HttpTransportSE(WSDL);
try {
Log.i("mylog", "info---" + httpTranstation);//日志打印到这里,就转入下面catch (IOException e)部分
httpTranstation.call(targetNameSpace+login, envelope);
Log.i("mylog", "info");
if(envelope.getResponse() != null){
SoapObject soapObject = (SoapObject) envelope.getResponse();
Log.i("mylog", "用户名--------"+soapObject.getProperty("name"));
Log.i("mylog", "密码--------"+soapObject.getProperty("pwd"));
reutrnResult = "ok";
}
} catch (IOException e) {
Log.e("mylog", "error------1111");
e.printStackTrace();
reutrnResult = "连接WSDL失败--1";//运行时android界面返回这个结果
} catch (XmlPullParserException e) {
Log.e("mylog", "error------2222");
reutrnResult = "连接WSDL失败--2";
e.printStackTrace();
}
return reutrnResult;
}
}
Logcat error message:
first a system error message Connection failed: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8181): connect failed: ECONNREFUSED ; (Connection refused)
android interface:
This time Tomcat is the start state, I can access the page to refresh http://localhost:8181/webTest/services/IPersonImp?wsdl wsdl, but why Android will fail to connect it?
Another problem ( second question, the first question has nothing to do with the )
webTest works inside IPersonImp class, when it inside getCompanyList method (returning a collection), click the right button and select "Web Services" -> "Create Web Service" warning will appear automatically create a webservice, What is the reason ah? If only the login method (return String) on the matter, anyway, you can not return the collection
IPersonImp code:
public class IPersonImp implements IPerson {
PersonDAO comdao=new PersonDAO();
//得到所有用户列表
public List<Person> getCompanyList() {
List<Person> list=new ArrayList<Person>();
try {
list=comdao.getCompanyList();
} catch (SQLException e) {
e.printStackTrace();
list=null;
}
return list;
}
public String login(String uname, String pwd) {
// TODO Auto-generated method stub
Person com = new Person();
com = comdao.login(uname, pwd);
String result = "fail";
if(com != null){
result = "success";
}
return result;
}
}
warning shots:
------ Solution ------- -------------------------------------
virtual machine to access the machine, without being able to localhost or 127.0.0.1, starting 10.0.0.2.
------ For reference only -------------------------------------- -
Thank you, I changed to 10.0.2.2 localhost can login. Phone emulator but if not, then I can use this method to test the local localhost services?
------ For reference only -------------------------------------- -
Also, could you help me look at the second question is how is it?
Why return to the List collection, select "Web Services" -> "Create Web Service" will appear automatically created webservice screenshot above warning?
------ For reference only - --------------------------------------
landlord made at the source look white? recent But this one is looking webservice read ah, or do not know how to access webservice
没有评论:
发表评论