0

I'm trying to fetch list of services from web service and add it to autocompletetextview. So suppose if you enter "a", you will get the below list of services in the autocompletetextview. I think its a json array but I'm getting error "Json error: Value Body Spa at 0 of type java.lang.String cannot be converted to JSONArray". Please correct me if I'm doing this the wrong way.

// you get this json response on entering "a"

[
  "Body Spa",
  "Hair Cut",
  "Hair massage",
  "fghfghfgh",
  "rtyrt",
  "Gold Facial",
  "Foot's massage"
]
public class HomeOptionTwo extends Fragment implements TextWatcher{

    AutoCompleteTextView autoservice;
    String[] list;
    ArrayAdapter<String> adapter;

    private static final String SERVICE = "http://192.168.200.1/android/spaservice";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.homeoptiontwo, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        autoservice = (AutoCompleteTextView)view.findViewById(R.id.service);


        autoservice.addTextChangedListener(this);





    }

    private void prepareMyList() {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, SERVICE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try {
                            //JSONObject jObj = new JSONObject(response);

                            JSONArray jsonarray = new JSONArray(response);
                            for (int i = 0; i < jsonarray.length(); i++) {
                                Toast.makeText(getActivity(), "services:" + jsonarray.getJSONArray(i), Toast.LENGTH_SHORT).show();

                                adapter = new ArrayAdapter<String>(
                                        getActivity(),
                                        android.R.layout.simple_dropdown_item_1line,
                                        list);

                                autoservice.setAdapter(adapter);
                            }





                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("'phrase", autoservice.getText().toString());
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        prepareMyList();

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
Somnath Pal
  • 1,570
  • 4
  • 23
  • 42
  • JSONArray is containing list of strings, you are fetching array from array which is incorrect. Change your toast to `Toast.makeText(getActivity(), "services:" + jsonarray.getString(i), Toast.LENGTH_SHORT).show();` – Ashwini May 23 '16 at 05:42
  • you dont need any `TextWatcher` / `addTextChangedListener` nor async voley requests, see http://stackoverflow.com/a/19860624/2252830 – pskink May 23 '16 at 06:07
  • I'm using volley as the web service filters the total list of 36 items according to the text entered. Now my current prob is I'm getting all 36 items even when I enter some text which should reduce the item list. @pskink – Somnath Pal May 23 '16 at 06:37
  • did you see the link i posted? – pskink May 23 '16 at 06:38
  • Yes, but I want volley request as I get different items on different text entered(filteration is done at API level). I got what's the error now. I'm getting the items list with 4 times repetition. @pskink – Somnath Pal May 23 '16 at 06:50
  • did you check when `runQuery` is called? watch the logcat when you are entering some text in ACTV, just copy/paste those ~30 lines of code to your `onCreate` method and run – pskink May 23 '16 at 06:52
  • I think I need to clear the adapter as if I don't previous list will get added to my current list. Can you tell me where I should use `adapter.clear()` in my current code? – Somnath Pal May 23 '16 at 07:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112647/discussion-between-somnath-pal-and-pskink). – Somnath Pal May 23 '16 at 07:16

1 Answers1

1

Here:

 Toast.makeText(getActivity(), 
     "services:" + jsonarray.getJSONArray(i), Toast.LENGTH_SHORT).show();

jsonarray contains String as item instead of JSONArray. use optString or getString to get value from jsonarray JSONArray using index. like:

ArrayList<String> arrList=new ArrayList<String>();
for (int i = 0; i < jsonarray.length(); i++) {
   String strValue=jsonarray.optString(i);                             
   arrList.add(strValue);
 }
  adapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_dropdown_item_1line,
                arrList);

  autoservice.setAdapter(adapter);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213