SharedPreferences의 Json 객체 세션 부분 문자열 대체

7707 단어
SharedPreferences에서 Json 대상session의 일부 문자열 교체, 일부 문자열 업데이트와 관련된, 서버의 데이터를 다시 로드하고 싶지 않음
구현 방법:
package X;XX

XXX

public class C_My1InfoPopWindows extends C_BaseActivity {
	
	private EditText editText;
	private String fromText,fromType,fromDefaultVal;
    private String session;
    JSONObject sessionJson;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.correcting_my_info_modify);
		
		initDataAndView();
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void initDataAndView() {

		editText = (EditText) findViewById(R.id.editText);
		fromText = getIntent().getStringExtra("text");
		fromType = getIntent().getStringExtra("type");
		fromDefaultVal = getIntent().getStringExtra("defaultText");
		
		editText.setText(fromText);
		/** */
		CharSequence text = editText.getText();
		if (text instanceof Spannable) {
			Spannable spanText = (Spannable)text;
			Selection.setSelection(spanText, text.length());
		}
		
		switch (fromType) {
		
		case "name": 
			editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
		break;
		case "phone": 
			editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
			editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL);
			break;
		case "email": 
			editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});
			editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
			break;
		case "company": 
			editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50)});
			break;
		
	}
		
	}
	
	public void save(View v) {
		
		String saveStr = editText.getText().toString().trim();
		
		if (StringUtil.isEmpty(saveStr)) {
			// , 
			new SaveDataAsynctask().execute(fromDefaultVal);
			return;
			
		} else {
			
			// 
			
			String regexEmail = "^[\\w-]+(\\.[\\w-]+)*\\@([\\.\\w-]+)+$";
			String regexPhone = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
			String regex =  (fromType.equals("phone")) ? regexPhone : (fromType.equals("email")) ? regexEmail : null;
			
			// 
			if (regex != null) {
				
				boolean isPass = Pattern.matches(regex, saveStr);
				
				if (!isPass) {
					
					Toast.makeText(this, " !", Toast.LENGTH_SHORT).show();
					return;
					
				}
				
			}  
				
			new SaveDataAsynctask().execute(saveStr);
			
		}
		
		
	}
	
	private class SaveDataAsynctask extends AsyncTask<String, Void, String> {
		
		@Override
		protected String doInBackground(String... params) {

			String isRet = null;
			
			//http://115.29.238.140:8017/api.ashx?fdBuyeIP=14.150.164.199&fdBuyeName=qwww&method=Buyer.Update&userUUID=f0ff399d-4a4e-4a8a-8e72-73bff8d08e90&fdBuyeUnit= &fdBuyeStatus=0&fdBuyeEmail=&fdAutoId=0&fdBuyeId=193&propStatusName= &fdBuyeMobile=15521392526&fdBuyeImage=http://115.29.238.140:8017/plugin/upload/web/store/demand/635866545788924671.png&fdBuyeAddress=
			//"http://115.29.238.140:8017/api.ashx?"
			//fdBuyeIP=14.150.164.199&
			/*fdBuyeName=qwww&
			method=Buyer.Update&
			userUUID=f0ff399d-4a4e-4a8a-8e72-73bff8d08e90&
			fdBuyeUnit= &
			fdBuyeStatus=0&
			fdBuyeEmail=&
			fdAutoId=0&
			fdBuyeId=193&
			propStatusName= &
			fdBuyeMobile=15521392526&
			fdBuyeImage=http://115.29.238.140:8017/plugin/upload/web/store/demand/635866545788924671.png&
			fdBuyeAddress=*/
			
			try {
				session = getSharedPreferences(Properties.LOGIN_INFO, 0).getString("o2OSetSession", null);
				
				if(session == null) return null;
				sessionJson = new JSONObject(session); 
				String detail = sessionJson.getString("detail");
				JSONObject detailJson = new JSONObject(detail);
				
				String propStatusName , fdBuyeId ,fdBuyeMobile , 
					   fdBuyeEmail , fdBuyeUnit ,fdBuyeAddress ,
					   fdBuyeName ,fdBuyeIP ,fdBuyeStatus ,fdBuyeImage ,fdAutoId;
				
				propStatusName = detailJson.getString("propStatusName");
				fdBuyeId = detailJson.getString("fdBuyeId");
				fdBuyeMobile = detailJson.getString("fdBuyeMobile");
				fdBuyeEmail = detailJson.getString("fdBuyeEmail");
				fdBuyeUnit = detailJson.getString("fdBuyeUnit");
				fdBuyeAddress = detailJson.getString("fdBuyeAddress");
				fdBuyeName = detailJson.getString("fdBuyeName");
				fdBuyeIP = detailJson.getString("fdBuyeIP");
				fdBuyeStatus = detailJson.getString("fdBuyeStatus");
				fdBuyeImage = detailJson.getString("fdBuyeImage");
				fdAutoId = detailJson.getString("fdAutoId");
				
				switch (fromType) {
				
					case "name": 
						fdBuyeName = params[0];
						detailJson.put("fdBuyeName", fdBuyeName);
					break;
					case "phone": 
						fdBuyeMobile =  params[0];
						detailJson.put("fdBuyeMobile", fdBuyeMobile);
						break;
					case "email": 
						fdBuyeEmail =  params[0];
						detailJson.put("fdBuyeEmail", fdBuyeEmail);
						break;
					case "company": 
						fdBuyeUnit =  params[0];
						detailJson.put("fdBuyeUnit", fdBuyeUnit);
						break;
					
				}
				
				sessionJson.put("detail", detailJson);
				
				StringBuilder paramBuider = new StringBuilder(); 
				paramBuider.append("fdBuyeIP=" + fdBuyeIP);
				paramBuider.append("&fdBuyeName=" + fdBuyeName);
				paramBuider.append("&method=" + "Buyer.Update");
				paramBuider.append("&userUUID=" + C_My1InfoPopWindows.this.getSharedPreferences(Properties.LOGIN_INFO,0).getString(Properties.LoginInfo.USERUUID, ""));
				paramBuider.append("&fdBuyeUnit=" + fdBuyeUnit);
				paramBuider.append("&fdBuyeStatus=" + fdBuyeStatus);
				paramBuider.append("&fdBuyeEmail=" + fdBuyeEmail);
				paramBuider.append("&fdAutoId=" + fdAutoId);
				paramBuider.append("&fdBuyeId=" + fdBuyeId);
				paramBuider.append("&propStatusName=" + propStatusName);
				paramBuider.append("&fdBuyeMobile=" + fdBuyeMobile);
				paramBuider.append("&fdBuyeImage=" + fdBuyeImage);
				paramBuider.append("&fdBuyeAddress=" + fdBuyeAddress);
				
				String uri = "/api.ashx?"; 
				isRet = HttpManage.httpPost(C_My1InfoPopWindows.this.getApplicationContext(), 
						Properties.SERVER_URL_TEST + uri, paramBuider.toString(),null);
			} catch (Exception e) {
			}
			

			return isRet;
			
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			
			if (result == null) {

				Toast.makeText(C_My1InfoPopWindows.this, " , !", Toast.LENGTH_SHORT).show();
				return;
				
			} else {
			
				try {
					
					String errStr = new JSONObject(result).getString("error");
					String msg = new JSONObject(errStr).getString("err_msg");
					int code = Integer.parseInt( new JSONObject(errStr).getString("err_code") );
					
					if (code == 1 ) {
						
						// 
						Toast.makeText(C_My1InfoPopWindows.this, msg, Toast.LENGTH_SHORT).show();
						getSharedPreferences(Properties.LOGIN_INFO, 0).edit().putString("o2OSetSession",sessionJson.toString()).commit();
						C_My1InfoPopWindows.this.finish();
						
					} else {
						
						Toast.makeText(C_My1InfoPopWindows.this, msg, Toast.LENGTH_SHORT).show();
						return;
						
					}
				} catch (Exception e) {
				}
				
			}
			
		}
		
	}
	
}

좋은 웹페이지 즐겨찾기