我正在尝试从Web服务获取服务列表并将其添加到autocompletetextview。因此,假设您输入"a"
,您将在autocompletetextview中获得以下服务列表。我认为它是一个json数组,但我收到错误"Json error: Value Body Spa at 0 of type java.lang.String cannot be converted to JSONArray".
如果我这样做的话,请纠正我。
// 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) {
}
}
答案 0 :(得分:1)
下面:
Toast.makeText(getActivity(),
"services:" + jsonarray.getJSONArray(i), Toast.LENGTH_SHORT).show();
jsonarray
包含String作为项而不是JSONArray。使用optString
或getString
使用索引从jsonarray
JSONArray获取值。像:
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);
答案 1 :(得分:0)
如果您想将数据添加到String [] arr = new String [100];那么它是错误的。 您可以执行与ArrayList形式相同的工作,但请记住,您永远不会在这里放置Getter / Setter类。只需声明一下即可。看到这个例子。
在主分区中声明:
ArrayList<String>arr=new ArrayList<>();
,然后以这种方式将其初始化:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
String imei = jsonObject1.getString("imei");
String name = jsonObject1.getString("name");
String my_pic = jsonObject1.getString("my_pic");
String email = jsonObject1.getString("email");
arr.add(name);
}
adapter= new ArrayAdapter<>
(this, android.R.layout.select_dialog_item, arr);
autoCompleteText.setThreshold(1);//will start working from first character
autoCompleteText.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
autoCompleteText.setTextColor(Color.RED);
}
我希望这对您有用...