首页 > 解决方案 > 获取异常: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;

        }
    }
}

请帮忙。我看到了有关此类异常的其他解决方案,但没有任何效果。我不明白的是,相同的代码非常适用于“计算”,但不适用于“登录”。为什么?

标签: androidksoap2

解决方案


HttpTransportSE使用请求设置重试超时

HttpTransportSE http = new HttpTransportSE(URL, 30000);

推荐阅读