我正在尝试从数据库中检索JSONObject
,但我发现该请求甚至没有进入服务器。我检查了Apache Access Log
以确认这一点。 URL有效,当我通过Web浏览器访问它时,它返回expect JSONObject。 Volley库显示异常错误null
。
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);
}
我该如何解决这个问题?
注意
服务器在我的本地计算机上,我通过热点连接http://192.164.***.***
。
答案 0 :(得分:2)
尝试从Android移动设备的浏览器做一个请求,如果它完全正常,那么必须是凌空问题。 如果它不起作用那么这意味着你只能输入192.168就无法连接到服务器.... 还要检查此链接并在模拟器中运行应用程序: How to connect to my http://localhost web server from Android Emulator in Eclipse
所以,无论如何你想使用这样的服务器使你的服务器上线。并使用您的全局IP来ping服务器。 阅读如何根据您自己的服务器文档将本地服务器联机。
答案 1 :(得分:2)
我的策略是,制作Volley单身并从任何地方使用它。
这是步骤:
在build.gradle
文件中添加此gradle
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
像这样创建应用程序类
public class YourApplication extends Application {
public static final String TAG = YourApplication.class.getSimpleName();
private RequestQueue requestQueue;
private com.android.volley.toolbox.ImageLoader imageLoader;
private static YourApplication instace;
@Override
public void onCreate() {
super.onCreate();
instace = this;
}
public static synchronized YourApplication getInstance() {
return instace;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (requestQueue != null) {
requestQueue.cancelAll(tag);
}
}
}
在menifest.xml
<application
android:name=".package_path.YourApplication">
在清单中添加互联网权限
<uses-permission android:name="android.permission.INTERNET" />
现在你的截击配置了。你只需这样使用它:
final ProgressDialog progressDialog = new ProgressDialog(this, ProgressDialog.THEME_HOLO_LIGHT);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
URL_HERE,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject object) {
progressDialog.dismiss();
// Your Data is here
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
//Error Occured
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
YourApplication.getInstance().addToRequestQueue(jsonObjectRequest, "YOUR_TAG");
修改强>
在build.gradle文件中使用compile 'com.android.volley:volley:1.0.0''
。
compile 'com.mcxiaoke.volley:library-aar:1.0.0' is just deprecated.
就是这样:))
答案 2 :(得分:1)
getMessage()有时不起作用。尝试此代码以获取服务器返回的完整错误消息。
if (volleyError != null && volleyError.networkResponse != null) {
int statusCode = volleyError.networkResponse.statusCode;
Log.w(tagToUse, TAG + " response code:" + statusCode + "");
byte[] data = volleyError.networkResponse.data;
int length = data.length;
StringBuilder stringBuilder = new StringBuilder(length);
for (int x = 0; x < length; x++) {
stringBuilder.append(((char) data[x]));
}
String errorMessage = stringBuilder.toString();
Log.d(tagToUse, TAG + " Server error data: " + errorMessage);
}
答案 3 :(得分:1)
这可能是由于URL不安全所致,因此您可以在AndroidManifest.xml中添加此行
android:usesCleartextTraffic =“ true”
<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">
,如果出现此错误[java.lang.NoClassDefFoundError:失败的解决方案:Lorg / apache / http / ProtocolVersion]错误,则将该行添加为应用程序标记子项。
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
有关更多详细信息,请访问此链接-
https://developer.android.com/about/versions/pie/android-9.0-changes-28#apache-p
答案 4 :(得分:-1)
兄弟首先检查API(应用程序编程接口),您的响应是否在 Json 中,然后下一步是使用 volley。我认为你的 API 不正确