gpt4 book ai didi

java - (Android Java Sqlite)由于IP地址导致连接数据库失败时如何提示toast

转载 作者:行者123 更新时间:2023-12-02 03:29:49 26 4
gpt4 key购买 nike

如果成功插入数据库,则 toast 消息有效,但如果例如我更改为无效的主机 IP 地址,则不会显示“无法创建配置文件”toast 消息

这是我的代码:

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);
}
}

最佳答案

我不知道确切的代码,但尝试连接时应该会出现异常。异常名称为UnknownHostException。您可以在连接逻辑周围放置 try catch,如果您遇到上述异常,那么它一定是由于 ip cahnge 而您可以处理它。

关于java - (Android Java Sqlite)由于IP地址导致连接数据库失败时如何提示toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com