Я создаю проект с использованием представлений Android Recycle, чтобы перечислить названия индийских штатов и районов.Представление Recycler для состояния работает нормально.Однако, когда я пытаюсь отправить полученный текст из представления переработчика на сервер, используя запрос строки залпа, это выдает мне ошибку: «Значение no типа java.lang.String не может быть преобразовано в JSONObject».Я не могу понять, что не так.Кто-нибудь может помочь мне с этим?Я новичок в Android и JSON.Я попробовал счетчики и списки, чтобы сделать это действие.Это выдает ту же ошибку, когда я пытаюсь отправить текст, полученный из адаптера.
Метод, где я получаю ошибку:
private void loadDistricts() {
RequestQueue queue = Volley.newRequestQueue(States.this);
StringRequest request = new StringRequest(Request.Method.GET, url_districts,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray statesArray = obj.getJSONArray("districts");
for(int i=0; i<statesArray.length(); i++) {
JSONObject stateObject = statesArray.getJSONObject(i);
String DtHindi = stateObject.getString("DtHindi");
String DTName = stateObject.getString("DTName");
String District11 = stateObject.getString("District11");
District districts = new District(DtHindi, DTName, District11);
dist_list.add(districts);
districtAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(States.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(States.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("state", state.getText().toString());
return map;
}
};
queue.add(request);
}
Весь код действия:
public class States extends AppCompatActivity {
EditText state, district;
RecyclerView state_recycler_view, district_recycler_view;
StateAdapter stateAdapter;
DistrictAdapter districtAdapter;
RecyclerView.LayoutManager stateLayoutManager, distLayoutManager;
List<State> state_list = new ArrayList<>();
List<District> dist_list = new ArrayList<>();
String getState, getDistrict;
String url_states = "http://subdomain.portalsamples.esy.es/states";
String url_districts = "http://subdomain.portalsamples.esy.es/districts";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_states);
state = (EditText) findViewById(R.id.state);
district = (EditText) findViewById(R.id.district);
state_recycler_view = (RecyclerView) findViewById(R.id.state_recycler_view);
stateAdapter = new StateAdapter(state_list);
stateLayoutManager = new LinearLayoutManager(getApplicationContext());
state_recycler_view.setLayoutManager(stateLayoutManager);
district_recycler_view = (RecyclerView) findViewById(R.id.district_recycler_view);
districtAdapter = new DistrictAdapter(dist_list);
distLayoutManager = new LinearLayoutManager(getApplicationContext());
district_recycler_view.setLayoutManager(distLayoutManager);
state.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadStates();
state_recycler_view.setAdapter(stateAdapter);
state_recycler_view.setVisibility(View.VISIBLE);
}
});
district.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadDistricts();
district_recycler_view.setAdapter(districtAdapter);
district_recycler_view.setVisibility(View.VISIBLE);
}
});
state_recycler_view.addOnItemTouchListener(new RecyclerTouchListener(States.this, state_recycler_view, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
state.setText(((TextView) state_recycler_view.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.STName)).getText().toString());
state_list.clear();
state_recycler_view.setVisibility(View.GONE);
}
@Override
public void onLongClick(View view, int position) {
district.setText(((TextView) district_recycler_view.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.DTName)).getText().toString());
dist_list.clear();
district_recycler_view.setVisibility(View.GONE);
}
}));
}
private void loadStates() {
RequestQueue queue = Volley.newRequestQueue(States.this);
StringRequest request = new StringRequest(Request.Method.GET, url_states,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray statesArray = obj.getJSONArray("states");
for(int i=0; i<statesArray.length(); i++) {
JSONObject stateObject = statesArray.getJSONObject(i);
String StHindi = stateObject.getString("StHindi");
String STName = stateObject.getString("STName");
String State11 = stateObject.getString("State11");
State states = new State(StHindi, STName, State11);
state_list.add(states);
stateAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(States.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(States.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
queue.add(request);
}
private void loadDistricts() {
RequestQueue queue = Volley.newRequestQueue(States.this);
StringRequest request = new StringRequest(Request.Method.GET, url_districts,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray statesArray = obj.getJSONArray("districts");
for(int i=0; i<statesArray.length(); i++) {
JSONObject stateObject = statesArray.getJSONObject(i);
String DtHindi = stateObject.getString("DtHindi");
String DTName = stateObject.getString("DTName");
String District11 = stateObject.getString("District11");
District districts = new District(DtHindi, DTName, District11);
dist_list.add(districts);
districtAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(States.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(States.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("state", state.getText().toString());
return map;
}
};
queue.add(request);
}