gpt4 book ai didi

android - com.android.volley.NoConnectionError : javax.net.ssl.SSLException : Connection closed by peer on 4. 4.4 设备(适用于 7.0.1)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:11:55 24 4
gpt4 key购买 nike

我在使用 volley 从 Web 服务获取数据到微调器时遇到了问题。我的问题是同样的事情在 Android 7.0.1(模拟器) 中有效,但在 Android 4.4 中无效(真实设备)我现在能做什么。请帮帮我。我的代码是这样的

public class MainActivity 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;
private RequestQueue requestQueue;

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


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

private void getData(){


//Creating a request queue

JsonObjectRequest StringRequest = new JsonObjectRequest(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);



}

});
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 = 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


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

}

//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {

}

}

最佳答案

我遇到过类似的问题,即我的 API 调用可以在较新版本的 Android(我认为是 21 及更高版本)中运行,但不能在较旧的版本中运行。这是因为只有较新版本的 Android 才支持较新版本的 SSL。

为了克服这个问题,我们必须在初始化 Volley 请求队列时进行更多设置:

 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(context, stack);
} else {
requestQueue = Volley.newRequestQueue(context);
}

TLSSocketFactory

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;
}
}

TLSSocketFactory 类添加到您的项目中,它应该可以完美地工作。

关于android - com.android.volley.NoConnectionError : javax.net.ssl.SSLException : Connection closed by peer on 4. 4.4 设备(适用于 7.0.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42999914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com