如何在ksoap Android-studio中配置网址?

时间:2016-07-15 17:43:21

标签: android web-services android-ksoap2

我想要可配置的网址,用户可以使用正在运行的应用程序更改它

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;
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以将该网址存储在偏好设置中。

创建首选项屏幕 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