我是 android 的新手,不知道我采用的方法是对是错。我想做的是通过单击按钮将两个编辑文本和一个微调器的值传递给 API 发布请求,然后从创建的 API 的响应中获取所需的通行费列表,并在单击的按钮下方填充 recyclerview。但是 API 返回状态 500,即验证错误告诉三个字段是必需的。谁能告诉我我哪里做错了在完成那个任务。 这是我的 OneTimePaymentActivity.java
public class OneTimePayActivity extends AppCompatActivity {
private Toolbar toolbar;
private EditText etSource, etDest;
private Spinner spinner;
private AppCompatButton payNow;
private LoginActivity LA;
private RecyclerView otp_rec_view;
private ArrayList<OneTimePayModel> oneTimePayModelArrayList;
private String textFromSpinner, etSourceText, etDestText, spinnerItemText, url = "https://tollsuvidha-api.000webhostapp.com/public/api/users/toll-list";
private int id;
private float tollAmtfloat;
private String tollName, city, tollAmountString;
OneTimePayModel oneTimePayModel;
ProgressDialog progressDialog;
private TextView tvFromSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one_time_pay);
etSource = (EditText) findViewById(R.id.etSource);
etDest = (EditText) findViewById(R.id.etDest);
tvFromSpinner = (TextView) findViewById(R.id.textFromSpinner);
toolbar = (Toolbar) findViewById(R.id.toolbarReg);
setSupportActionBar(toolbar);
oneTimePayModelArrayList = new ArrayList<>();
otp_rec_view = (RecyclerView) findViewById(R.id.otp_rec_view);
otp_rec_view.setHasFixedSize(true);
oneTimePayModel = new OneTimePayModel();
progressDialog = new ProgressDialog(OneTimePayActivity.this);
progressDialog.setTitle("Please Wait...");
progressDialog.setMessage("Posting data");
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
otp_rec_view.setLayoutManager(llm);
spinner = (Spinner) findViewById(R.id.spinner1);
etSource = (EditText) findViewById(R.id.etSource);
etDest = (EditText) findViewById(R.id.etDest);
payNow = (AppCompatButton) findViewById(R.id.paynowButton);
LA = new LoginActivity();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.dd_vehicle_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerItemText = parent.getItemAtPosition(position).toString();
tvFromSpinner.setText(spinnerItemText);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
payNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
etSourceText = etSource.getText().toString();
etDestText = etDest.getText().toString();
textFromSpinner = tvFromSpinner.getText().toString();
if (!LA.isNetworkAvailable(OneTimePayActivity.this)) {
Toast.makeText(OneTimePayActivity.this, "Please check your internet connection", Toast.LENGTH_LONG).show();
} else if (etSourceText.equals("") || etDestText.equals("")) {
Toast.makeText(getApplicationContext(), "Fields are empty", Toast.LENGTH_LONG).show();
} else {
sendRequest();
}
}
private void sendRequest() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
progressDialog.show();
int status = response.getInt("status");
if (status == 200) {
Toast.makeText(OneTimePayActivity.this, "Payment Success", Toast.LENGTH_LONG).show();
String message = response.getString("message");
JSONArray data = response.getJSONArray("data");
oneTimePayModelArrayList.clear();
for (int i = 0; i < data.length(); i++) {
JSONObject jobj = data.getJSONObject(0);
id = jobj.getInt("id");
tollName = jobj.getString("toll_name");
city = jobj.getString("city");
tollAmountString = jobj.getString(textFromSpinner + "_price");
tollAmtfloat = Float.parseFloat(tollAmountString);
oneTimePayModel.setId(id);
oneTimePayModel.setTollHead(tollName);
oneTimePayModel.setTollLocation(city);
oneTimePayModel.setTollAmount(tollAmtfloat);
oneTimePayModelArrayList.add(oneTimePayModel);
}
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} else if (status == 500) {
String message = response.getString("message");
Toast.makeText(OneTimePayActivity.this, message, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
OneTimePayAdapter adapter = new OneTimePayAdapter(oneTimePayModelArrayList);
otp_rec_view.setAdapter(adapter);
adapter.notifyDataSetChanged();
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("source", etSourceText);
params.put("destination", etDestText);
params.put("vehicle_type", textFromSpinner);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MyApplication.getContext());
requestQueue.add(jsonObjectRequest);
}
});
}
}
我什至尝试调试应用程序并查看传递给 API 的文本的值。但即使传递的文本不为空,它也总是返回 status=500(验证错误)而不是 status=200(响应成功)。 这是我上述 Activity 的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolBarOTP"
layout="@layout/titlebar" />
<TextView
android:id="@+id/onetimeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolBarOTP"
android:text="@string/one_time_payment"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textFromSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/onetimeText" />
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputSource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textFromSpinner"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/etSource"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:hint="@string/source"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/textInputDest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textInputSource">
<EditText
android:id="@+id/etDest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:hint="@string/dest"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textInputDest"
android:drawSelectorOnTop="true" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/paynowButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_marginTop="20dp"
android:text="@string/pay_now"
android:textSize="20sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/otp_rec_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/paynowButton"
android:layout_marginTop="10dp" />
</RelativeLayout>
这里是 activity_one_time_pay.xml 的布局截图:
我花了 3 个小时努力寻找解决方案,但找不到任何具体的内容。请告诉我正确的方法。
最佳答案
在 String 变量中获取值并将该变量设置为类的全局变量。 你应该试试这个:
String spinnerItemText = parent.getItemAtPosition(position).toString();
想要整数值:
Int value=Integer.parseInt(spinnerItemText).
关于java - Spinner.getSelectedItem().toString 返回字符串,但在 API 发布请求中传递时变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45242229/