繁体   English   中英

com.android.volley.ServerError 上 Android 工作室

[英]com.android.volley.ServerError on Android studio

我的应用程序很简单,主要活动只是 go 到相关活动的 4 个按钮,其他 4 个活动用于查看、添加、更新和删除产品。 我有一个名为“AppController”的文件,其中我的连接“已定义”,这是它的代码:


import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;


//this is a singleton class where we initialize all volley core objects
public class AppController extends Application{
    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    public static String baseUrl= "https://example.000webhostapp.com/";  
/** Not the actual link */

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getmInstance(){
        return mInstance;
    }

    public RequestQueue getRequestQueue(){
        if(mRequestQueue == null){
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req){
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag){
        if(mRequestQueue != null){
            mRequestQueue.cancelAll(tag);
        }
    }

} 

我已经确定我的服务器正在使用 Postman 工作,我使用它添加了一个产品,并且它工作正常。

我已经检查了其他帖子,但无法将其与我的代码相关联(我是 android 开发人员的新手)

更新,我让它与最后添加到我的 AppController 文件中的以下代码一起工作(只是帮助将来可能需要它的人)

public void onErrorResponse(VolleyError error) {

                    NetworkResponse response = error.networkResponse;
                    if (response != null && response.statusCode == 404) {
                        try {
                            String res = new String(response.data,
                                    HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                            // Now you can use any deserializer to make sense of data
                            JSONObject obj = new JSONObject(res);
                            //use this json as you want
                        } catch (UnsupportedEncodingException e1) {
                            // Couldn't properly decode data to string
                            e1.printStackTrace();
                        } catch (JSONException e2) {
                            // returned data is not JSONObject?
                            e2.printStackTrace();
                        }
                    }
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM