Just started learning Android, many do not know the place. This problem has not found reason:
Information found on the Internet to connect remote database use Android WebService, get the WebService data koap2.
I am here to do a simple test landing:
I use Eclipse built a Dynamic Web Project Project: webTest, write the relevant code, connect to the database test is successful. After the file name of the class IPersonImp right click and select "Web Services" -> "Create Web Service" to automatically create webservice:
Under the project will be deployed to Tomcat, start Tomcat, enter the URL in the browser, can be found wsdl:
Browser Test landing effect (the password is wrong, the result is fail):
problem :
Create a new 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 document namespace
private static final String targetNameSpace = "http://service.main";
/ / WSDL document URL
private static final String WSDL = "http://localhost:8181/webTest/services/IPersonImp";
/ / Method name you want to call (get Myervices the login method)
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) ;/ / log print here, it transferred to the following catch (IOException e) section
httpTranstation.call (targetNameSpace + login, envelope);
Log.i ("mylog", "info");
if (envelope.getResponse ()! = null) {
SoapObject soapObject = (SoapObject) envelope.getResponse ();
Log.i ("mylog", "username --------" + soapObject.getProperty ("name"));
Log.i ("mylog", "password --------" + soapObject.getProperty ("pwd"));
reutrnResult = "ok";
}
} Catch (IOException e) {
Log.e ("mylog", "error ------ 1111");
e.printStackTrace ();
reutrnResult = "Failed to connect WSDL - 1" ;/ / android interface returns the result
runtime} Catch (XmlPullParserException e) {
Log.e ("mylog", "error ------ 2222");
reutrnResult = "Failed to connect WSDL - 2";
e.printStackTrace ();
}
return reutrnResult;
}
}
Logcat error message:
The first one is 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 connection fail?
Another problem ( second question, nothing to do with the first question )
webTest works inside IPersonImp class when it inside getCompanyList method (returns a collection), right click and choose "Web Services" -> "Create Web Service" warning will appear automatically created webservice, why ah? If only the login method (returns String) on the matter, anyway, you can not return the collection
IPersonImp code:
public class IPersonImp implements IPerson {
PersonDAO comdao = new PersonDAO ();
/ / Get a list of all users
public ListgetCompanyList () {
Listlist = new ArrayList ();
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:

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Virtual machine to access the machine, not able to localhost or 127.0.0.1, starting 10.0.0.2.
Reply:
Thank you, I changed localhost 10.0.2.2 can login. If not, the simulator but the phone, I can use this method to test the local localhost services?
Reply:
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?
Reply:
The landlord made about the source look white? Recently watching webservice but can not read this one, ah, still do not know how to access webservice
Reply:
The landlord, the phone connection is not solved that problem yet
Reply:
Because I use the simulator, rather than a real machine, you can not use localhost access
According to # 1 floor to reply to localhost 10.0.2.2 has been resolved
No comments:
Post a Comment