Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数据未显示在spinner中使用截击-真实设备问题_Java_Android - Fatal编程技术网

Java 数据未显示在spinner中使用截击-真实设备问题

Java 数据未显示在spinner中使用截击-真实设备问题,java,android,Java,Android,在中测试时,代码运行良好,但在spinner中测试相同代码时,代码未显示。甚至没有错误消息 Config.java public class Config { public static String mLocation ="http://towncitycards.com/webservice_action.php?action="; //http://towncitycards.com/webservice_action.php?action=filter_location public

在中测试时,代码运行良好,但在spinner中测试相同代码时,代码未显示。甚至没有错误消息

Config.java

public class Config {

public static String mLocation ="http://towncitycards.com/webservice_action.php?action=";
//http://towncitycards.com/webservice_action.php?action=filter_location
public static final String DATA_URL = mLocation+"filter_location";

//Tags used in the JSON String
public static final String TAG_USERNAME = "name";
public static final String TAG_NAME = "slug";
public static final String TAG_COURSE = "name";
public static final String TAG_SESSION = "slug";

//JSON array name
public static final String JSON_ARRAY = "location";
}
public class DemoSpinner extends AppCompatActivity implements Spinner.OnItemSelectedListener{

//Declaring an Spinner
private Spinner spinner;

//An ArrayList for Spinner Items
private ArrayList<String> students;

//JSON Array
private JSONArray result;

//TextViews to display details
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);

    //Initializing the ArrayList
    students = new ArrayList<String>();

    //Initializing Spinner
    spinner = (Spinner) findViewById(R.id.spinner);

    //Adding an Item Selected Listener to our Spinner
    //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
    spinner.setOnItemSelectedListener(this);

    //Initializing TextViews
    textViewName = (TextView) findViewById(R.id.textViewName);
    textViewCourse = (TextView) findViewById(R.id.textViewCourse);
    textViewSession = (TextView) findViewById(R.id.textViewSession);

    //This method will fetch the data from the URL
    getData();
}

private void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(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
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getStudents(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
            students.add(json.getString(Config.TAG_USERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, students));
}

//Method to get student name of a particular position
private String getName(int position){
    String name="";
    try {
        //Getting object of given index
        JSONObject json = result.getJSONObject(position);

        //Fetching name from that object
        name = json.getString(Config.TAG_NAME);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Returning the name
    return name;
}

//Doing the same with this method as we did with getName()
private String getCourse(int position){
    String course="";
    try {
        JSONObject json = result.getJSONObject(position);
        course = json.getString(Config.TAG_COURSE);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return course;
}

//Doing the same with this method as we did with getName()
private String getSession(int position){
    String session="";
    try {
        JSONObject json = result.getJSONObject(position);
        session = json.getString(Config.TAG_SESSION);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return session;
}


//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    textViewName.setText(getName(position));
    textViewCourse.setText(getCourse(position));
    textViewSession.setText(getSession(position));
}

//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
    textViewName.setText("");
    textViewCourse.setText("");
    textViewSession.setText("");
}
}
DemoSpinner.java

public class Config {

public static String mLocation ="http://towncitycards.com/webservice_action.php?action=";
//http://towncitycards.com/webservice_action.php?action=filter_location
public static final String DATA_URL = mLocation+"filter_location";

//Tags used in the JSON String
public static final String TAG_USERNAME = "name";
public static final String TAG_NAME = "slug";
public static final String TAG_COURSE = "name";
public static final String TAG_SESSION = "slug";

//JSON array name
public static final String JSON_ARRAY = "location";
}
public class DemoSpinner extends AppCompatActivity implements Spinner.OnItemSelectedListener{

//Declaring an Spinner
private Spinner spinner;

//An ArrayList for Spinner Items
private ArrayList<String> students;

//JSON Array
private JSONArray result;

//TextViews to display details
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);

    //Initializing the ArrayList
    students = new ArrayList<String>();

    //Initializing Spinner
    spinner = (Spinner) findViewById(R.id.spinner);

    //Adding an Item Selected Listener to our Spinner
    //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
    spinner.setOnItemSelectedListener(this);

    //Initializing TextViews
    textViewName = (TextView) findViewById(R.id.textViewName);
    textViewCourse = (TextView) findViewById(R.id.textViewCourse);
    textViewSession = (TextView) findViewById(R.id.textViewSession);

    //This method will fetch the data from the URL
    getData();
}

private void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(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
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getStudents(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
            students.add(json.getString(Config.TAG_USERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, students));
}

//Method to get student name of a particular position
private String getName(int position){
    String name="";
    try {
        //Getting object of given index
        JSONObject json = result.getJSONObject(position);

        //Fetching name from that object
        name = json.getString(Config.TAG_NAME);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Returning the name
    return name;
}

//Doing the same with this method as we did with getName()
private String getCourse(int position){
    String course="";
    try {
        JSONObject json = result.getJSONObject(position);
        course = json.getString(Config.TAG_COURSE);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return course;
}

//Doing the same with this method as we did with getName()
private String getSession(int position){
    String session="";
    try {
        JSONObject json = result.getJSONObject(position);
        session = json.getString(Config.TAG_SESSION);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return session;
}


//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    textViewName.setText(getName(position));
    textViewCourse.setText(getCourse(position));
    textViewSession.setText(getSession(position));
}

//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
    textViewName.setText("");
    textViewCourse.setText("");
    textViewSession.setText("");
}
}
公共类DemoSpinner扩展AppCompative实现Spinner.OnItemSelectedListener{
//宣布成为纺纱工
私人纺纱机;
//微调器项的ArrayList
私立ArrayList学生;
//JSON数组
私人JSONArray结果;
//文本视图以显示详细信息
私有文本视图文本视图名称;
私人TextView TextView课程;
私有文本视图文本视图会话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
//初始化ArrayList
学生=新数组列表();
//初始化微调器
微调器=(微调器)findViewById(R.id.spinner);
//向微调器添加选定项侦听器
//由于我们已经将类Spinner.OnItemSelectedListener实现到这个类本身,我们将把它传递给setOnItemSelectedListener
spinner.setOnItemSelectedListener(此);
//初始化文本视图
textViewName=(TextView)findViewById(R.id.textViewName);
textViewCourse=(TextView)findViewById(R.id.textViewCourse);
textViewSession=(TextView)findViewById(R.id.textViewSession);
//此方法将从URL获取数据
getData();
}
私有void getData(){
//创建字符串请求
StringRequest StringRequest=新的StringRequest(Config.DATA\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
JSONObject j=null;
试一试{
//将获取的Json字符串解析为Json对象
j=新的JSONObject(响应);
//将JSON字符串数组存储到我们的JSON数组中
result=j.getJSONArray(Config.JSON_数组);
//调用getStudents方法从JSON数组中获取学生
获取学生(结果);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
//创建请求队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
//将请求添加到队列
添加(stringRequest);
}
私人学生(JSONArray j){
//遍历json数组中的所有项
对于(int i=0;i父级){
textViewName.setText(“”);
textViewCourse.setText(“”);
textViewSession.setText(“”);
}
}

StringRequest-StringRequest=new-StringRequest(Request.Method.GET,Config.DATA\u URL,new-Response.Listener()

在getData()中使用它,因为未指定Request.Method.GET

::或: 您可以这样使用:

new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "filter_location");
            return params;
        }
    };
newresponse.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“动作”、“过滤器位置”);
返回参数;
}
};

然后从url中删除参数“action=filter\u location”。

StringRequest StringRequest=new StringRequest(Request.Method.GET,Config.DATA\u url,new Response.Listener()

在getData()中使用它,因为未指定Request.Method.GET

::或: 您可以这样使用:

new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("action", "filter_location");
            return params;
        }
    };
newresponse.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“动作”、“过滤器位置”);
返回参数;
}
};

然后从url中删除参数“action=filter\u location”。

请使用此编辑的代码,这将适用于您:

public class DemoSpinner extends AppCompatActivity implements Spinner.OnItemSelectedListener{

//Declaring an Spinner
private Spinner spinner;

//An ArrayList for Spinner Items
private ArrayList<String> students;

//JSON Array
private JSONArray result;

//TextViews to display details
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);

    //Initializing the ArrayList
    students = new ArrayList<String>();

    //Initializing Spinner
    spinner = (Spinner) findViewById(R.id.spinner);

    //Adding an Item Selected Listener to our Spinner
    //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
    spinner.setOnItemSelectedListener(this);

    //Initializing TextViews
    textViewName = (TextView) findViewById(R.id.textViewName);
    textViewCourse = (TextView) findViewById(R.id.textViewCourse);
    textViewSession = (TextView) findViewById(R.id.textViewSession);

    //This method will fetch the data from the URL
    getData();
}

   private void getData(){


        //Creating a request queue

        JsonObjectRequest StringRequest = new JsonObjectRequest(Request.Method.GET, Config.DATA_URL, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null) {
                            JSONObject j = null;
                            try {
                                //Parsing the fetched Json String to JSON Object
                                j = 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
                                getStudents(result);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("Volly error is this >>" + error);



                    }

                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(StringRequest);
    }


    private void getStudents(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
                students.add(json.getString(Config.TAG_USERNAME));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        Log.e("student >>",students.toString());
        //Setting adapter to show the items in the spinner
        spinner.setAdapter(new ArrayAdapter<String>(this,  android.R.layout.simple_spinner_item, students));
    }


//Method to get student name of a particular position
private String getName(int position){
    String name="";
    try {
        //Getting object of given index
        JSONObject json = result.getJSONObject(position);

        //Fetching name from that object
        name = json.getString(Config.TAG_NAME);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Returning the name
    return name;
}

//Doing the same with this method as we did with getName()
private String getCourse(int position){
    String course="";
    try {
        JSONObject json = result.getJSONObject(position);
        course = json.getString(Config.TAG_COURSE);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return course;
}

//Doing the same with this method as we did with getName()
private String getSession(int position){
    String session="";
    try {
        JSONObject json = result.getJSONObject(position);
        session = json.getString(Config.TAG_SESSION);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return session;
}


//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    textViewName.setText(getName(position));
    textViewCourse.setText(getCourse(position));
    textViewSession.setText(getSession(position));
}

//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
    textViewName.setText("");
    textViewCourse.setText("");
    textViewSession.setText("");
}
}
公共类DemoSpinner扩展AppCompative实现Spinner.OnItemSelectedListener{
//宣布成为纺纱工
私人纺纱机;
//微调器项的ArrayList
私立ArrayList学生;
//JSON数组
私人JSONArray结果;
//文本视图以显示详细信息
私有文本视图文本视图名称;
私人TextView TextView课程;
私有文本视图文本视图会话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
//初始化ArrayList
学生=新数组列表();
//初始化微调器
微调器=(微调器)findViewById(R.id.spinner);
//向微调器添加选定项侦听器
//由于我们已经将类Spinner.OnItemSelectedListener实现到这个类本身,我们将把它传递给setOnItemSelectedListener
spinner.setOnItemSelectedListener(此);
//初始化文本视图
textViewName=(TextView)findViewById(R.id.textViewName);
textViewCourse=(TextView)findViewById(R.id.textViewCourse);
textViewSession=(TextView)findViewById(R.id.textViewSession);
//此方法将从URL获取数据
getData();
}
私有void getData(){
//创建请求队列
JsonObjectRequest StringRequest=新的JsonObjectRequest(Request.Method.GET,Config.DATA\u URL,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
if(响应!=null){
JSONObject j=null;
试一试{
//将获取的Json字符串解析为Json对象