之前我在android studio上编写了一个从Android设备上使用Web服务的代码。
网络服务链接 - http://202.54.216.49/logs/test.asmx
首先,我使用代码来使用计算方法,它完全正常。现在我使用相同的代码来使用loginauth方法,它给出了异常:java.net.SocketTimeoutException并获得NULL结果。我使用了以下代码 -
package com.example.abhimanyu.devicecontrol;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends AppCompatActivity {
EditText textBox, textBox2;
Button button;
TextView ans;
String URL = "http://202.54.216.49/logs/test.asmx";
String NAMESPACE = "http://tempuri.org/";
String SOAP_ACTION = "http://tempuri.org/loginauth";
String METHOD_NAME = "loginauth";
String PARAMETER_NAME1 = "username";
String PARAMETER_NAME2 = "password";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText) findViewById(R.id.txtBox);
textBox2 = (EditText) findViewById(R.id.txtBox2);
button = (Button) findViewById(R.id.btn);
ans = (TextView) findViewById(R.id.answer);
final Context context;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CallWebService().execute(textBox.getText().toString(), textBox2.getText().toString());
}
});
}
class CallWebService extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String s) {
ans.setText(s);
}
@Override
protected void onPreExecute() {
Log.i("onPreexecute","running");
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String result = null;
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo propertyInfo1 = new PropertyInfo();
propertyInfo1.setName(PARAMETER_NAME1);
propertyInfo1.setValue(params[0]);
propertyInfo1.setType(String.class);
soapObject.addProperty(propertyInfo1);
PropertyInfo propertyInfo2 = new PropertyInfo();
propertyInfo2.setName(PARAMETER_NAME2);
propertyInfo2.setValue(params[1]);
propertyInfo2.setType(String.class);
soapObject.addProperty(propertyInfo2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes=true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
httpTransportSE.call(SOAP_ACTION, envelope);
SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
result = soapPrimitive.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
}
请帮助。我看到了有关此类异常的其他解决方案,但没有任何效果。而我不明白的是,相同的代码适用于“计算”,但不适用于“loginauth”。为什么呢?
答案 0 :(得分:0)
使用HttpTransportSE
请求
HttpTransportSE http = new HttpTransportSE(URL, 30000);