问题
I'm new to android and I wanted to call wcf web service. I have found ksoap2 library and stated to apply the examples I found.The app failed with the following error message: java.net.SocketTimeoutException: Connection timeout. I had set user permission on the manafist file like that:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I have also add the proxy, port, username and password from the emulator setting app-> wireless and network->mobile networks->Access Point Names. I have also added the following line from run configuration to additional emulator command line options:
-dns-server ns15.unitechost.in
But still it did not work, any help would be appreciated.
Update
I have configured the proxy setting from command line when starting AVD with the following command:
emulator -avd <avd_name> [-<option> [<value>]] ... [-<qemu args>]
Now I got the following exception:
org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT HTTP/1.1 500 Int...@11:1 in java.io.InputStreamReader@40531d48)
I have seen examples for consuming .asmx web service using ksoap2, can this be done on .svc web service as well? I'm using the attached code, I got from one if the examples:
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
private static final String SOAP_ACTION = "http://tempuri.org/Istock/getCountry";
private static final String OPERATION_NAME = "getCountry";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://127.0.0.1:8080/Service1.svc";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try
{
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
textView.setText(response.toString());
System.out.println(response.toString());
}
catch (Exception exception)
{
String exceptionStr=exception.toString();
textView.setText(exceptionStr);
System.out.println(exceptionStr);
Log.i("TAG",exceptionStr);
}
}
}
Thanks,
回答1:
Connection timeouts occur when:
- The IP address for the requested server is successfully found
- Connection establishment packets are dispatched to the IP address
- The destination address deliberately ignores or does not receive them
Similar to connection timeout is Connection Refused, but in this case the destination system is actually sending packets back saying "go away, there is no service running on the port you are trying to connect to"
Connection timed out is an expected behavior in a network environment. It's even more expected in a mobile environment, where network connections can be unreliable. It means that the server failed to respond for a certain number of seconds.
回答2:
You cannot access your local machine by using localhost or 127.0.0.1 from android. You need to use 10.0.2.2 to access yout local machine.
Actually I didn't get the android to connect to a WCF service correctly...
回答3:
Although this may be a little late, this is what fixed it for me:
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);
Notice the second parameter, which is the timeout value.
I must admit tough, that i do not know the scale to go with this value, but I think this is measured in milliseconds.
回答4:
add this :
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
来源:https://stackoverflow.com/questions/5921170/invoke-wcf-web-service-using-ksoap2-on-android-app-failed-with-the-following-err