public class getToken extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}@Override
protected String doInBackground(String... strings) {
try {
postTextandGetRespons("xxxxx/Service/register.php");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String responseStr) {
String[] parts = responseString.split(":");
success = parts[0];
token = parts[1];
if ( success.equals("success")) {
SaveToken();
}
}
public void SaveToken() {
SharedPreferences token_save = getSharedPreferences("token_key", register.MODE_PRIVATE);
SharedPreferences.Editor editior2 = token_save.edit();
editior2.putInt("token",Integer.valueOf(token));
editior2.commit();
}
}
答案 0 :(得分:1)
SharedPreferences
正在使您的应用崩溃,但您的doInBackground
返回null的事实。使您的应用崩溃的行是
String[] parts = responseString.split(":");
因为doInBackground
确实
return null;
}
如果postTextandGetRespons
返回一个你应该做的字符串
@Override
protected String doInBackground(String... strings) {
try {
return postTextandGetRespons("xxxxx/Service/register.php");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
并在onPostExecute
再次检查NPE
。 E.g。
@Override
protected void onPostExecute(String responseStr) {
if (TextUtils.isEmpty(responseStr)) {
Log.e("", " responseStr null");
return;
}
String[] parts = responseString.split(":");