Monday, January 11, 2016

Android WebService connection fails, help me find the reason




            


Just started learning Android, many do not know the place. This issue has been not find the reason:
Information found on the Internet to connect remote database to use Android WebService, get the WebService data koap2.
I have here is a simple test to make a 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 IPersonImp class right click and select "Web Services" - & gt; "Create Web Service" to automatically create webservice:

Will deploy to the Tomcat project, start Tomcat, enter the URL in the browser, can be found wsdl:

Test browser landing effect (the password is wrong, return results fail):

question :
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 needs to call (to obtain 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 to part here, transferred the following catch (IOException e)
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 error message is connected to a system failure: 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 refresh http: // localhost:? 8181 / webTest / services / IPersonImp wsdl page can access the wsdl, but why Android connection fail?


Another problem ( The second problem, the problem has nothing to do with the first )
webTest IPersonImp class engineering inside, when it contains getCompanyList method (returns a collection), right click and select "Web Services" - & gt; "Create Web Service" to automatically create webservice warning appears, is what ah? If only the login process (Returns String) it does not matter, anyway, it can not return the collection
IPersonImp Code:
 
public class IPersonImp implements IPerson {
PersonDAO comdao = new PersonDAO ();
// Get list of all users
public List & lt; Person & gt; getCompanyList () {
List & lt; Person & gt; list = new ArrayList & lt; Person & gt; ();
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:

Reply:
Virtual machine access to the machine, without energy localhost or 127.0.0.1, starting 10.0.0.2.
Reply:
quote 1 floor a_mean reply:
a virtual machine to access the machine, not able to localhost or 127.0.0.1, starting 10.0.0.2.

Thank you, I put localhost can log on to 10.0.2.2. If not, the simulator but the phone, I can use this method to test local localhost services?
Reply:
quote 1 floor a_mean reply:
a virtual machine to access the machine, not able to localhost or 127.0.0.1, starting 10.0.0.2.

Also, could you help me look at the top of the second question is how is it?
Why return List collection, select "Web Services" - & gt; "Create Web Service" to automatically create webservice above screenshot will appear in the warning?
Reply:
The landlord made about the source look white? Webservice recently looking at this piece ah but can not read, or do not know how to access the webservice
Reply:
The landlord, the phone connection is not yet on that issue is resolved
Reply:
quote 5th floor zengwenman reply:
the landlord, the phone connection is not on that problem has been solved yet

Since I was using the simulator, rather than a real machine, can not be accessed by localhost
According to # 1 floor to reply to the localhost 10.0.2.2 has been resolved
Reply:
This and your machine is provided with a relationship, a little complicated, if necessary, please contact me qq: 1302557666

No comments:

Post a Comment