我正在尝试从android中的微调器中获取字符串值,并将其发布到MySQL数据库。我正在从数据库中获取正在填充微调器的数据,然后使用齐射将其附加一些其他数据。问题是当我尝试将其发布时,该应用由于某种原因而崩溃。
此处是代码段。 Spinner值是一个哈希图,其中将包含来自Spinner的值
jsonSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
namas = jsonSpinner.getItemAtPosition(jsonSpinner.getSelectedItemPosition()).toString();
spinnerValue.put("value",namas);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Heres the code to post the data using volley
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotBranchName = spinnerValue.get("value");
gotVenue = venue.getText().toString();
gotService = service.getText().toString();
gotDate = date.getText().toString();
gotNumberOfMales = numOfMales.getText().toString();
gotNumberOfFemales = numOfFemales.getText().toString();
gotNumberOfChildren = numOfChildren.getText().toString();
if (gotVenue.equals("") || gotService.equals("") || gotDate.equals("") ||
gotNumberOfMales.equals("") || gotNumberOfFemales.equals("") || gotNumberOfChildren.equals("")) {
CookieBar.build(MainActivity.this)
.setTitle("Error")
.setMessage("cannot post data. check for empty fields")
.show();
} else {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
//send the values to the database as key value pairs
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jo = new JSONObject(response);
int status = jo.getInt("status");
if (status == 1) {
CookieBar.build(MainActivity.this)
.setTitle("Success")
.setMessage("Record Added ")
.show();
clearFields();
} else {
CookieBar.build(MainActivity.this)
.setTitle("Error")
.setMessage("Could Not Add Data")
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
CookieBar.build(MainActivity.this)
.setTitle("Error")
.setMessage("Please check your connection settings")
.show();
Log.i("My error", "" + error);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("branchName", gotBranchName);
map.put("venue", gotVenue);
map.put("service", gotService);
map.put("date", gotDate);
map.put("numberOfMales", gotNumberOfMales);
map.put("numberOfFemales", gotNumberOfFemales);
map.put("numberOfChildren", gotNumberOfChildren);
return map;
}
};
queue.add(request);
}
}
答案 0 :(得分:0)
请从
更改此行namas = jsonSpinner.getItemAtPosition(jsonSpinner.getSelectedItemPosition()).toString();
到
namas = jsonSpinner.getSelectedItem().toString();