真实设备上的问题(适用于7.0.1仿真器)

时间:2017-03-25 01:37:26

标签: java android android-volley

我遇到了使用volley从web服务获取数据到spinner的问题。我的问题与 Android 7.0.1(模拟器)相同但在 Android 4.4(真实设备)中没有,我现在该怎么办?

我的网络服务: https://www.towncitycards.com/webservice_action.php?action=filter_location

public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener{

//Declaring an Spinner
private Spinner spinner;
private RequestQueue requestQueue;

//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.activity_main);

    //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) {

                }
            });
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
            && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
        HttpStack stack = null;
        try {
            stack = new HurlStack(null, new TLSSocketFactory());
        } catch (KeyManagementException e) {
            e.printStackTrace();
            Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
            stack = new HurlStack();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
            stack = new HurlStack();
        }
        requestQueue = Volley.newRequestQueue(getApplicationContext(), stack);
    } else {
        requestQueue = Volley.newRequestQueue(getApplicationContext());
        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>(MainActivity.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("");
}
}

TLSSocketFactory.java

public class TLSSocketFactory extends SSLSocketFactory {

private SSLSocketFactory internalSSLSocketFactory;

public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, null, null);
    internalSSLSocketFactory = context.getSocketFactory();
}

@Override
public String[] getDefaultCipherSuites() {
    return internalSSLSocketFactory.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
    return internalSSLSocketFactory.getSupportedCipherSuites();
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
    return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}

private Socket enableTLSOnSocket(Socket socket) {
    if(socket != null && (socket instanceof SSLSocket)) {
        ((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
    }
    return socket;
}
}

log

0 个答案:

没有答案