第一次从另一个活动中的共享首选项中读取数据时获取Null,第二次显示值已经通过intent将整个json对象传递给另一个活动但是没有解决我的问题我已经尝试过使用齐射库请帮忙
public class MainActivity extends AppCompatActivity {
TextInputEditText name, addresssss, address1111, city, state, country, postalcode, landmark,mobiles;
TextView namee, addresss, address11, cityy, statee, countryy, postalcodee, landmarkk,hhhh;
String nameee, nam, ad, ad1, ci, sta, cou, pos, land, nameeee;
Button Submit;
String ggg;
JSONObject object;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
namee = (TextView) findViewById(R.id.namee);
landmarkk = (TextView) findViewById(R.id.landmarkk);
addresss = (TextView) findViewById(R.id.addresss);
address11 = (TextView) findViewById(R.id.address11);
cityy = (TextView) findViewById(R.id.cityy);
statee = (TextView) findViewById(R.id.statee);
countryy = (TextView) findViewById(R.id.countryy);
postalcodee = (TextView) findViewById(R.id.postalcodee);
hhhh=(TextView)findViewById(R.id.hhhh);
mobiles=(TextInputEditText)findViewById(R.id.mobiles);
name = (TextInputEditText) findViewById(R.id.name);
addresssss = (TextInputEditText) findViewById(R.id.address);
address1111 = (TextInputEditText) findViewById(R.id.address1);
city = (TextInputEditText) findViewById(R.id.city);
state = (TextInputEditText) findViewById(R.id.state);
country = (TextInputEditText) findViewById(R.id.country);
postalcode = (TextInputEditText) findViewById(R.id.postalcode);
landmark = (TextInputEditText) findViewById(R.id.landmark);
Submit = (Button) findViewById(R.id.Submit);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new SendRequest().execute();
nameee = name.getText().toString().trim();
if (TextUtils.isEmpty(name.getText().toString())) {
return;
}
ad = addresssss.getText().toString();
if (TextUtils.isEmpty(addresssss.getText().toString())) {
return;
}
ad1 = address1111.getText().toString();
if (TextUtils.isEmpty(address1111.getText().toString())) {
return;
}
ci = city.getText().toString();
if (TextUtils.isEmpty(city.getText().toString())) {
return;
}
sta = state.getText().toString();
if (TextUtils.isEmpty(state.getText().toString())) {
return;
}
cou = country.getText().toString();
if (TextUtils.isEmpty(country.getText().toString())) {
return;
}
pos = postalcode.getText().toString();
if (TextUtils.isEmpty(postalcode.getText().toString())) {
return;
}
land = landmark.getText().toString().trim();
if (TextUtils.isEmpty(landmark.getText().toString())) {
return;
}
nameeee=mobiles.getText().toString().trim();
if (TextUtils.isEmpty(mobiles.getText().toString())) {
return;
}
else {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
});
}
public class SendRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
}
protected String doInBackground(String... arg0) {
try {
URL url = new URL("http://services.sweken.com/api2/information/checkRegionByPincode");
JSONObject postDataParams = new JSONObject();
postDataParams.put("address", ad);
postDataParams.put("address_1", ad1);
postDataParams.put("pincode", pos);
postDataParams.put("land_mark", land);
postDataParams.put("name", nameee);
postDataParams.put("mobile_no", nameeee);
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds *);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
} else {
return new String("false : " + responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
try {
object = new JSONObject(result);
ggg = object.getString("customer_id");
Log.d("customer",ggg);
SharedPreferences.Editor editt = sharedPreferences.edit();
editt.putString("CUSTOMER_ID", ggg);
editt.apply();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while (itr.hasNext()) {
String key = itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
这是我从共享偏好中读取值的第二个Activity我第一次第二次读取值时变为null
public class Main2Activity extends AppCompatActivity {
String idddd;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
text=(TextView)findViewById(R.id.text);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
`idddd = sharedPreferences.getString`("CUSTOMER_ID","");
text.setText(idddd);
}
}
答案 0 :(得分:0)
听起来像竞争条件,因为写入SharedPreference发生在AsyncTask的末尾。没有看到更多代码就很难分辨。尝试放置断点,或者在写入断点后尝试直接读取共享首选项,或创建OnSharedPreferenceChangeListener以确保将数据全部写入。