android - 获取异常:java.net.SocketTimeoutException
问题描述
早些时候,我编写了一个代码来在 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;
}
}
}
请帮忙。我看到了有关此类异常的其他解决方案,但没有任何效果。我不明白的是,相同的代码非常适用于“计算”,但不适用于“登录”。为什么?
解决方案
HttpTransportSE
使用请求设置重试超时
HttpTransportSE http = new HttpTransportSE(URL, 30000);
推荐阅读
- google-cloud-platform - 在 IAP 中使用“云身份验证登录”进行身份验证后,如何限制用户仅使用他的用户名(通过该用户名进行身份验证)?
- facebook-graph-api - 如何使用 Graph API 从公共页面获取帖子?
- r - 如何重新排列 R 数据框中不同列中的值
- javascript - 在反应中,我有 JQuery npm,我在我的 chart-custom.js 文件中使用了 JQuery,但仍然收到 jQuery undefined 错误
- python - 导入后覆盖模块函数 - Python
- vba - VBA selenium 单击附近的网格列
- python - 使用 str.contains 创建新列 Pandas df 给出:值的长度与索引的长度不匹配
- c# - 如何根据 SQLite DB 表中的表制作 Picker 显示选项?
- node.js - 带有温度传感器的 SocketIO - 我几乎用 NodeJS 和 SocketIO 在我的 Raspberry Pi 上完成了 DS18B20,如何提取 Promise { data }?
- google-maps - 颤动谷歌地图缩放控制位置