如何使 url 在 ksoap Android-studio 中可配置?

How can I make the url configurable in the ksoap Android-studio?

我想要可配置的 URL ,用户可以使用 运行 application

更改它
public String NAMESPACE ="http://tempuri.org/";
public String url; // **How can I make the url configurable**
public int timeOut = 60000;
public IWsdl2CodeEvents eventHandler;
public SoapProtocolVersion soapVersion;

我尝试了一个会话变量,一个外部文件,现在使用了 sqlite,但是当我尝试调用该过程并将其作为参数方法告知并为从数据库中提取的 url 赋值时,我的应用程序停止。

这是 getUrl

public String getUrl() {
    String url = null;

    SQLiteHelper sql = new SQLiteHelper(Voceo.this,
            "servicioweb", null, 1);
    SQLiteDatabase bd = sql.getWritableDatabase();
    Cursor fila = bd.rawQuery(" SELECT Url from direcciones where idUrl= 0", null);
    if (fila.moveToFirst()) {
       url = fila.getString(0);
    }
    bd.close();
    return url;
}

这是我试着给他打电话的时候

public VectorString ObtenerPuertas(List<HeaderProperty> headers){
    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.implicitTypes = true;
    soapEnvelope.dotNet = true;
    SoapObject soapReq = new SoapObject("http://tempuri.org/","ObtenerPuertas");
    soapEnvelope.setOutputSoapObject(soapReq);
    HttpTransportSE httpTransport = new HttpTransportSE(getUrl(),timeOut); // **Here I try**
    try{
        if (headers!=null){
            httpTransport.call("http://tempuri.org/ObtenerPuertas", soapEnvelope,headers);
        }else{
            httpTransport.call("http://tempuri.org/ObtenerPuertas", soapEnvelope);
        }
        Object retObj = soapEnvelope.bodyIn;
        if (retObj instanceof SoapFault){
            SoapFault fault = (SoapFault)retObj;
            Exception ex = new Exception(fault.faultstring);
            if (eventHandler != null)
                eventHandler.Wsdl2CodeFinishedWithException(ex);
        }else{
            SoapObject result=(SoapObject)retObj;
            if (result.getPropertyCount() > 0){
                Object obj = result.getProperty(0);
                SoapObject j = (SoapObject)obj;
                VectorString resultVariable = new VectorString(j);
                return resultVariable;
            }
        }
    }catch (Exception e) {
        if (eventHandler != null)
            eventHandler.Wsdl2CodeFinishedWithException(e);
        e.printStackTrace();
    }
    return null;
}

我做错了什么?

您可以将 URL 存储在首选项中。

创建首选项屏幕 preference.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


    <EditTextPreference
        android:capitalize="words"
        android:defaultValue="@string/pref_ip_default_value"     <------- Add that string in strings values (the default value for the ip)
        android:inputType="textCapWords"
        android:key="@string/pref_ip_key"        <------- Add that string in strings values (key for retrieve the ip)
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_title_ip" <------- Add that string in strings values (the label that will show for user)

/>


</PreferenceScreen>

SettingsActivity.java:

public class SettingsActivity extends PreferenceActivity
        implements Preference.OnPreferenceChangeListener {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_ip_key)));




        }

    }

    private void bindPreferenceSummaryToValue(Preference preference) {
        preference.setOnPreferenceChangeListener(this);

        onPreferenceChange(preference, PreferenceManager
                .getDefaultSharedPreferences(preference.getContext())
                .getString(preference.getKey(), ""));

    }


    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        preference.setSummary(stringValue);



        return true;
    }
}

在你的task.java中:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ActvityContext);   // <---- Get the preferences manager
 String pref_key = ActvityContext.getString(R.string.pref_ip_key);     // <------ get the name of preference 
 String pref_default = ActvityContext.getString(R.string.pref_ip_default_value);     //  < ---- get the value variable
 String preferedUrl = preferences.getString(pref_key, pref_default);    //   <------- get the user defined String

 Uri buildUri = Uri.parse(preferedUrl).buildUpon().build(); // <----- you can build an uri or do wherever you want