JSON 正在解析 GET 方法,尝试在 Spinner 中显示值
JSON Parsing GET method, trying to display values in Spinner
我试图通过使用 JSON 解析在 Spinner 中显示值,但它没有使用 centerID 并给我错误“找不到 centerID”。而且,微调器视图中没有显示任何内容。
// 这是我的 API 文件
BASE_URL : http://192.168.1.80/aoplnew/api/
API URL : BASE_URL/users/getdoctorslist/$center_id
METHOD : GET
PARAMETER TO PASS:
center_id=4
INPUT:
BASE_URL/users/getdoctorslist/4
OUTPUT:
{
"status": true,
"result": [
{
"doctor_id": "1",
"first_name": "Dr. Sagar",
"last_name": "Patil"
},
{
"doctor_id": "2",
"first_name": "Dr. Ashwini",
"last_name": "D"
}
],
"message": "Get Doctors list successfully!"
}
Errors:
1)
{
"status": false,
"message": "Center id should not empty!"
}
2)
{
"status": false,
"message": "There is no Center data found!"
}
这是我进行连接的配置文件
public class Config {
//JSON URL
public static final String DATA_URL = "http://192.168.1.80/aoplnew/api/users/getdoctorslist/{center_id}";
//Tags used in the JSON String
public static final String TAG_DOC_ID = "doctor_id";
public static final String TAG_FIRST_NAME = "first_name";
public static final String TAG_LAST_NAME = "last_name";
//JSON array name
public static final String JSON_ARRAY = "result";
}
这是我的片段,我在其中尝试使用 Volley 库获取 Spinner 中的值
private ArrayList<String> docList;
//JSON Array
private JSONArray result;
private Spinner doctorLists;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_patient, container, false);
doctorLists = (Spinner) view.findViewById(R.id.doctors_list);
//Initializing the ArrayList
docList = new ArrayList<String>();
//This method will fetch the data from the URL
getData();
return view;
} // closing onCreateView()
private void getData(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getDoctors(JSONArray j){
//Traversing through all the items in the json array
for(int i=0;i<j.length();i++){
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
docList.add(json.getString(Config.TAG_FIRST_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
doctorLists.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, docList));
}
您缺少请求方法
使用这个
你的Api需要在调用
时传递center_id作为参数
StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){ @Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("center_id", "4");
return params;
};
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}
如果还是不行请告诉我
StringRequest stringRequest = new StringRequest(Config.DATA_URL,...
您没有在请求中设置 center_id
参数
您已经有了关于您的 API 的信息。
API URL : BASE_URL/users/getdoctorslist/$center_id
METHOD : GET
PARAMETER TO PASS:
center_id=4
INPUT:
BASE_URL/users/getdoctorslist/4
您的 API 需要一个值 $center_id
但您没有传递该值。您要求 url 是
"http://192.168.1.80/aoplnew/api/users/getdoctorslist/{center_id}"
请发送值center_id。
试试这个
final String url = "http://192.168.1.80/aoplnew/api/users/getdoctorslist/4";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
try {
//Parsing the fetched JSON String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//添加请求到队列
requestQueue.add(字符串请求);
我英语不好
我建议使用 GSON 将 json 字符串转换为对象:
https://github.com/google/gson
并推荐使用改装来连接服务:
https://square.github.io/retrofit/
我试图通过使用 JSON 解析在 Spinner 中显示值,但它没有使用 centerID 并给我错误“找不到 centerID”。而且,微调器视图中没有显示任何内容。
// 这是我的 API 文件
BASE_URL : http://192.168.1.80/aoplnew/api/
API URL : BASE_URL/users/getdoctorslist/$center_id
METHOD : GET
PARAMETER TO PASS:
center_id=4
INPUT:
BASE_URL/users/getdoctorslist/4
OUTPUT:
{
"status": true,
"result": [
{
"doctor_id": "1",
"first_name": "Dr. Sagar",
"last_name": "Patil"
},
{
"doctor_id": "2",
"first_name": "Dr. Ashwini",
"last_name": "D"
}
],
"message": "Get Doctors list successfully!"
}
Errors:
1)
{
"status": false,
"message": "Center id should not empty!"
}
2)
{
"status": false,
"message": "There is no Center data found!"
}
这是我进行连接的配置文件
public class Config {
//JSON URL
public static final String DATA_URL = "http://192.168.1.80/aoplnew/api/users/getdoctorslist/{center_id}";
//Tags used in the JSON String
public static final String TAG_DOC_ID = "doctor_id";
public static final String TAG_FIRST_NAME = "first_name";
public static final String TAG_LAST_NAME = "last_name";
//JSON array name
public static final String JSON_ARRAY = "result";
}
这是我的片段,我在其中尝试使用 Volley 库获取 Spinner 中的值
private ArrayList<String> docList;
//JSON Array
private JSONArray result;
private Spinner doctorLists;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add_patient, container, false);
doctorLists = (Spinner) view.findViewById(R.id.doctors_list);
//Initializing the ArrayList
docList = new ArrayList<String>();
//This method will fetch the data from the URL
getData();
return view;
} // closing onCreateView()
private void getData(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getDoctors(JSONArray j){
//Traversing through all the items in the json array
for(int i=0;i<j.length();i++){
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
docList.add(json.getString(Config.TAG_FIRST_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
doctorLists.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, docList));
}
您缺少请求方法
使用这个
你的Api需要在调用
StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){ @Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("center_id", "4");
return params;
};
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}
如果还是不行请告诉我
StringRequest stringRequest = new StringRequest(Config.DATA_URL,...
您没有在请求中设置 center_id
参数
您已经有了关于您的 API 的信息。
API URL : BASE_URL/users/getdoctorslist/$center_id
METHOD : GET
PARAMETER TO PASS:
center_id=4
INPUT:
BASE_URL/users/getdoctorslist/4
您的 API 需要一个值 $center_id
但您没有传递该值。您要求 url 是
"http://192.168.1.80/aoplnew/api/users/getdoctorslist/{center_id}"
请发送值center_id。
试试这个
final String url = "http://192.168.1.80/aoplnew/api/users/getdoctorslist/4";
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
try {
//Parsing the fetched JSON String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getDoctors(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//添加请求到队列 requestQueue.add(字符串请求);
我英语不好
我建议使用 GSON 将 json 字符串转换为对象: https://github.com/google/gson
并推荐使用改装来连接服务: https://square.github.io/retrofit/