Android讀寫配置2

來源:互聯網
上載者:User

標籤:try   address   請求   target   gets   pac   exception   .json   ipa   

上篇文章採用 Properties 讀寫配置,各種路徑錯誤,要麼沒有寫入許可權.

後來查資料,採用另一種方式讀寫 SharedPreferencesImpl

直接貼代碼

公用類 -- 讀寫

package com.lemon.demo.utils;import android.content.Context;import android.content.SharedPreferences;import android.util.Log;public class UrlConfig {    private static SharedPreferences share;    private static String configPath = "appConfig";    public static SharedPreferences getProperties(Context context) {        try {            share = context.getSharedPreferences(configPath, Context.MODE_PRIVATE);        } catch (Exception e) {            e.printStackTrace();        }        return share;    }    public static String setProperties(Context context, String keyName, String keyValue) {        try {            share = context.getSharedPreferences(configPath, Context.MODE_PRIVATE);            SharedPreferences.Editor editor = share.edit();//取得編輯器            editor.putString(keyName, keyValue);//儲存配置 參數1 是key 參數2 是值            editor.commit();//提交重新整理資料        } catch (Exception e) {            e.printStackTrace();            Log.e("setPropertiesError", e.toString());            return "修改設定檔失敗!";        }        return "設定成功";    }}

 

封裝

package com.lemon.demo.json;import android.content.Context;import android.content.SharedPreferences;import com.lemon.demo.utils.UrlConfig;/** * 網路請求 */public class UrlString {    private static final String IP = "192.168.1.10:8000";    private String contrastIPName = "contrastIP";    // 上傳路徑    private String ip;    private String ipAddress;    public void setIPAddress(Context context) {        //Properties proper = ProperTies.getProperties(context);        //this.ip = proper.getProperty(contrastIPName, "");        SharedPreferences proper = UrlConfig.getProperties(context);        this.ip = proper.getString(contrastIPName, "");        // 設定預設值        if (this.ip.equals("")){            this.ip = IP;        }        this.ipAddress = "http://" + this.ip + "/v1.0/index.html";    }    public String setIPAddress(Context context, String keyValue) {        // String result = ProperTies.setProperties(context, contrastIPName, keyValue);        String result = UrlConfig.setProperties(context, contrastIPName, keyValue);        this.ip = keyValue;        this.ipAddress = "http://" + this.ip + "/v1.0/index.html";        return result;    }    public String getIP() {        return this.ip;    }    public String getIPAddress() {        return this.ipAddress;    }}

 

使用方式同上一篇.

參考:  http://www.jianshu.com/p/87024ce283c9

參考:  http://leequer.iteye.com/blog/654942

 

Android讀寫配置2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.