トーストメッセージは、データベースへの挿入が成功した場合に機能しますが、例えば無効なホストIPアドレスに変更すると、プロファイルを作成するために失敗し、」トーストメッセージがここに(Android Java Sqlite)IPアドレスのためにデータベースへの接続が失敗したときにトーストを促す方法
を示さなかった私のコードです:
public class RegistrationActivity extends AppCompatActivity implements onTaskCompleted {
Spinner greetingSpinner = null;
EditText edit_Name = null;
// TAG for debug log message
private final String TAG = this.getClass().getSimpleName();
// Declare variables for profile and response
String name;
String greeting;
String id;
String status;
String msgType;
private static final String REQ_UPLOAD = "1001";
private static final String REQ_DOWNLOAD = "1002";
// Set host address of the WAMP Server
public static final String HOST = "192.168.1.1";
// Set virtual directory of the host website
public static final String DIR = "myproject";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
greetingSpinner = (Spinner) findViewById(R.id.spinner_greeting);
edit_Name = (EditText) findViewById(R.id.edit_name);
}
/*public void onNextActivityClicked(View view)
{
saveAsPreferences();
Toast toast = Toast.makeText(this, "Profile created \n successfully!", Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if(v != null) v.setGravity(Gravity.CENTER);
toast.setGravity(Gravity.CENTER, 0, 0);
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(25);
toast.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
}, 2000);
}*/
// Convert profile information to JSON string
public String convertToJSON() {
JSONStringer jsonText = new JSONStringer();
try {
jsonText.object();
jsonText.key("type");
jsonText.value(msgType);
jsonText.key("greeting");
jsonText.value(greeting);
jsonText.key("name");
jsonText.value(name);
jsonText.key("id");
jsonText.value(id);
jsonText.endObject();
} catch (Exception e) {
e.printStackTrace();
}
return jsonText.toString();
}
// Retrieve profile information from JSON string
public void retrieveFromJSON(String message) {
try {
if(id != null) {
JSONObject jsonObject = new JSONObject(message);
msgType = jsonObject.getString("type");
if (msgType.equals(REQ_DOWNLOAD)) {
name = jsonObject.getString("name");
greeting = jsonObject.getString("greeting");
}
else if(id.equals(null)){
Toast toast = Toast.makeText(this, "Profile creation failed, \n please try again later!", Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.setGravity(Gravity.CENTER, 0, 0);
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(25);
toast.show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Handler for upload button click event
public void onNextActivityClicked(View v) {
// convert edit text values to string data
msgType = REQ_UPLOAD;
Spinner etGreeting = (Spinner) findViewById(R.id.spinner_greeting);
greeting = etGreeting.getSelectedItem().toString();
EditText etName = (EditText) findViewById(R.id.edit_name);
name = etName.getText().toString();
// create data in JSON format
String jsonString = convertToJSON();
// call AsyncTask to perform network operation on separate thread
HttpAsyncTask task = new HttpAsyncTask(this);
task.execute("http://" + HOST + "/" + DIR + "/registerProfile.php", jsonString);
}
// Handler for HTTPAsyncTask complete event
@Override
public void onTaskCompleted(String response) {
// retrieve response information from JSON
retrieveFromJSON(response);
// if response is from upload request
if (msgType.equals(REQ_UPLOAD)) {
SharedPreferences profile = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = profile.edit();
editor.putString("name", name);
editor.putString("greeting", greeting);
editor.commit();
Toast toast = Toast.makeText(this, "Profile created \n successfully!", Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.setGravity(Gravity.CENTER, 0, 0);
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(25);
toast.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
}, 2000);
}
}
「else if(id.equals(null)){」は意味をなさない。ちょうどそれを "else"にしてください。 – Fildor