当我尝试将某些内容保存到mlab上托管的mongoDB数据库中时,我的应用程序停止了。奇怪的是它没有错误地停止了,只是退出结束就带我回到了主屏幕。
我已经创建了一个类(仅带有setter和getter),用于创建稍后将保存在数据库中的对象。这些对象是在用户可以输入其数据的屏幕上创建和保存的。
//performing network call in background thread.
final class MongoLabSaveOrder extends AsyncTask<Object, Void, Boolean> {
@SuppressLint("LongLogTag")
@Override
protected Boolean doInBackground(Object... params) {
Myorders order = (Myorders) params[0];
Log.d("order", ""+order);
try {
SupportData sd = new SupportData();
URL url = new URL(sd.buildOrdersSaveURL());
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("PUT");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"application/json");
connection.setRequestProperty("Accept", "application/json");
OutputStreamWriter osw = new OutputStreamWriter(
connection.getOutputStream());
osw.write(sd.createContact(order));
osw.flush();
osw.close();
if(connection.getResponseCode() <205)
{
return true;
}
else
{
return false;
}
} catch (Exception e) {
e.getMessage();
Log.d("Got error", e.getMessage());
return false;
}
}
}
//the code from the onCreate() method
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calclay1);
Log.d(TAG, "onCreate: Starting.");
if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Button btnNext2 = (Button) findViewById(R.id.NextBtn);
btnNext2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: pressed button to go on page 3");
Myorders order=new Myorders();
EditText expedStreet=(EditText) findViewById(R.id.expedTextView);
EditText destStreet=(EditText) findViewById(R.id.destTextView);
Spinner sizespinner=(Spinner) findViewById(R.id.sizespin);
Spinner weightspinner=(Spinner) findViewById(R.id.weightspin);
RadioButton Express=(RadioButton) findViewById(R.id.radioButton3);
RadioButton Normal=(RadioButton)findViewById(R.id.radioButton4);
CheckBox Fragile=(CheckBox)findViewById(R.id.FragileCheck);
order.setSender_street(expedStreet.getText().toString());
order.setDest_street(destStreet.getText().toString());
order.setSizeP(sizespinner.getSelectedItem().toString());
order.setWeightP(weightspinner.getSelectedItem().toString());
if(Express.isChecked()) order.setExpr_Norm("Express");
else order.setExpr_Norm("Normal");
if(Fragile.isChecked()) order.setFragile("Yes");
else order.setFragile("Not");
//.........................
order.setSender_name("rares");
order.setDest_name("taciu");
order.setPhone_number("0899765568");
order.setEmail("taciu@gggg");
order.setDateP("2019");
order.setHour_P("09:29");
order.setSpecifications("delivered package");
MongoLabSaveOrder tsk=new MongoLabSaveOrder();
tsk.execute(order);
Toast.makeText(SecondScreen.this, "Saved to MongoDB!!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SecondScreen.this, ThirdScreen.class);
startActivity(intent);
按下此nxt按钮应读取微调器,EditText字段等的所有输入,并基于它们创建一个对象。该对象的值应保存在mlab的数据库中。 一些字段应该在下一步中完成,但是我的应用程序不会转到下一步,因此我只给了他们一些值。