簡體   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