简体   繁体   English

Android Volley请求将Recycler视图文本作为参数发布时出错

[英]Android Volley request Error on posting Recycler view text as parameter

I am creating a project using android recycler views to list the names of Indian states and districts. 我正在使用android回收站视图创建一个项目,以列出印度各州和地区的名称。 The Recycler view for state is working fine. 状态的Recycler视图工作正常。 However, when I try to post the retrieved text from recycler view to server using volley string request, it gives me error: "Value no of type java.lang.String cannot be converted to JSONObject." 但是,当我尝试使用截距字符串请求将检索到的文本从回收者视图发布到服务器时,它给了我错误:“ java.lang.String类型的值不能转换为JSONObject。” I am not able to figure out what is wrong. 我无法找出问题所在。 Can anyone please help me out with this? 谁能帮我这个忙吗? Iam new to Android and JSON. 我是Android和JSON的新手。 I have tried spinners and listviews as well to do this activity. 我也尝试过微调器和列表视图来完成此活动。 It gives the same error when I try to send the text retrieved from adapter. 当我尝试发送从适配器检索的文本时,它会给出相同的错误。

Method where I get the error: 我收到错误的方法:

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

Entire code of the activity: 活动的完整代码:

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

Sorry... The problem was with the api. 抱歉...问题出在api。 Unable to parse the response from server. 无法解析来自服务器的响应。 Was using laravel to create my api. 正在使用laravel创建我的api。 I have no idea why the response is always "empty input" when i use laravel to create the api. 我不知道为什么当我使用laravel创建api时,响应总是“空输入”。 Using core php for api solved it. 使用核心PHP的api解决了它。 However my question is still there for laravel scripts. 但是,对于laravel脚本,我的问题仍然存在。 Why doesn't it accept input when posting data using volley request ..... 为什么使用截击请求发布数据时它不接受输入...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM