我试图通过java(android)学习使用SOAP。 我按照this教程编写了以下代码:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends AppCompatActivity {
String TAG = "Response";
String value;
Button btn;
EditText val;
TextView fahr;
TextView cels;
SoapPrimitive result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.convert_btn);
val = (EditText) findViewById(R.id.value);
fahr = (TextView) findViewById(R.id.f);
cels = (TextView) findViewById(R.id.c);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View view) {
value = val.getText().toString();
AsyncCallWS ac = new AsyncCallWS();
ac.execute();
}
});
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
calculate();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
if (result != null) {
cels.setText(result.toString());
}
}
}
public void calculate() {
String SOAP_ACTION = "http://www.w3schools.com/xml/FahrenheitToCelsius";
String METHOD_NAME = "FahrenheitToCelsius";
String NAMESPACE = "http://www.w3schools.com/xml/";
String URL = "http://www.w3schools.com/xml/tempconvert.asmx";
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Fahrenheit", 15);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
result = (SoapPrimitive) soapEnvelope.getResponse();
Log.i(TAG, "Result Celsius: " + result);
} catch (Exception ex) {
ex.printStackTrace();
Log.e(TAG, "Error: " + ex.getMessage());
}
}
}
每当我运行它时,我在transport.call()
得到java.net.SocketTimeoutException
谷歌搜索和搜索SO并没有多大帮助。
我使用www.w3schools.com网络服务,通过浏览器完美加载网址。
注意: 如果我复制并粘贴示例中的代码,它将按预期工作。但是,如果我手动编写代码(大多数是相同的),它会抛出此异常。我从教程中改变的唯一方法是如何在UI上发布结果。
修改 堆栈跟踪如下:
java.net.SocketTimeoutException
07-05 06:51:16.094 2734-2777/com.kostas.tade W/System.err: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:130)
07-05 06:51:16.094 2734-2777/com.kostas.tade W/System.err: at com.kostas.tade.MainActivity.calculate(MainActivity.java:90)
07-05 06:51:16.094 2734-2777/com.kostas.tade W/System.err: at com.kostas.tade.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:60)
07-05 06:51:16.098 2734-2777/com.kostas.tade W/System.err: at com.kostas.tade.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:51)
07-05 06:51:16.107 2734-2777/com.kostas.tade W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
07-05 06:51:16.107 2734-2777/com.kostas.tade W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-05 06:51:16.110 2734-2777/com.kostas.tade W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-05 06:51:16.110 2734-2777/com.kostas.tade W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-05 06:51:16.110 2734-2777/com.kostas.tade W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-05 06:51:16.110 2734-2777/com.kostas.tade W/System.err: at java.lang.Thread.run(Thread.java:818)
实际calculate()
方法与示例(工作方法)完全相同。
不与this SO question重复,因为那个没有选择的答案,而且它会解决java.net.SocketTimeoutException : Read Timeout
答案 0 :(得分:0)
好的,想通了。由于某种原因打字
var _dateFormat = '';
//_dateFormat = '<%= WebUI.Models.WebConfigKeys.DateFormat %>';
_dateFormat = 'MM-dd-yyyy';
//var dateArray = '<%= ViewData["CompareCollectionDateSvc"] %>';
var dateArray = '07/05/2016,07/06/2016,07/07/2016,07/08/2016,07/11/2016';
dateArray = dateArray.split(',');
console.log(dateArray);
var _firstVal = dateArray[0];
console.log(_firstVal);
var _LastVal = dateArray[4];
console.log(_LastVal);
文件中的<uses-permission android:name="android.permission.INTERNET"/>
并未执行此操作。但是,复制并粘贴工作项目的权限,修复它。
我猜这是android studio上的一个错误。无论如何,谢谢你的帮助。