Bla*_*arl 1 android android-volley
我试图JSONObject从数据库中检索一个,但我发现该请求甚至没有发送到服务器。我检查了Apache Access Log确认这一点。URL 有效,当我通过 Web 浏览器访问它时,它返回预期的 JSONObject。nullVolley 库显示的异常错误。
private void GetSchInfo(){
showDialog();
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
URL_SchInfo, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
spinnerClass.setVisibility(View.VISIBLE);
spinnerSch.setVisibility(View.VISIBLE);
spinnerDept.setVisibility(View.VISIBLE);
btnRegClass.setVisibility(View.VISIBLE);
txtClassMsg.setVisibility(View.VISIBLE);
btnRefresh.setVisibility(View.GONE);
JSONArray schools = response.getJSONArray("sch_info");
JSONArray departments = response.getJSONArray("dept_info");
for (int i = 0; i < schools.length(); i++){
JSONObject schObj = (JSONObject)schools.get(i);
SchInfo sch = new SchInfo(schObj.getString("school_id"), schObj.getString("school_name"));
schoolList.add(sch);
}
for (int j = 0; j < departments.length(); j++){
JSONObject deptObj = (JSONObject)departments.get(j);
DeptItem dept = new DeptItem(deptObj.getString("department_id"), deptObj.getString("department_name"));
deptList.add(dept);
}
//populate
//populateInfo();
}catch(JSONException e){
Log.e(TAG, "JSON Error: " + e.getMessage());
e.printStackTrace();
spinnerClass.setVisibility(View.GONE);
txtClassMsg.setVisibility(View.GONE);
spinnerSch.setVisibility(View.GONE);
spinnerDept.setVisibility(View.GONE);
btnRegClass.setVisibility(View.GONE);
btnRefresh.setVisibility(View.VISIBLE);
}
hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String body;
if (error.networkResponse.data !=null){
try {
body = new String(error.networkResponse.data, "UTF-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
Log.e("Error: ", error.getCause() +">>" + error.getMessage());
Log.e(TAG, "JSON Error: " + error.getMessage() );
hideDialog();
spinnerClass.setVisibility(View.GONE);
spinnerSch.setVisibility(View.GONE);
spinnerDept.setVisibility(View.GONE);
btnRegClass.setVisibility(View.GONE);
txtClassMsg.setVisibility(View.GONE);
btnRefresh.setVisibility(View.VISIBLE);
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
注意
服务器在我的本地机器上,我通过热点连接类似http://192.164.***.***.
这可能是由于 URL 不安全,因此您可以在 AndroidManifest.xml 中添加这一行
机器人:使用CleartextTraffic =“真”
<application
android:name=".ExampleApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher_new"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_circle"
android:largeHeap="true"
android:resizeableActivity="false"
android:usesCleartextTraffic="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
如果它给出了 [java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion] 错误,则将此行添加为应用程序标记子项。
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
Run Code Online (Sandbox Code Playgroud)
欲了解更多详情,请访问此链接——
https://developer.android.com/about/versions/pie/android-9.0-changes-28#apache-p