android - অ্যান্ড্রয়েড/জাভা: কোনও পদ্ধতিতে ফিরতে কীভাবে বিলম্ব করবেন




json android-volley (2)

আমি ভলি কলকে আরও সহজ করার জন্য একটি ইউটিলস ক্লাস তৈরি করতে চাই:

Utils.java:

public class Utils {
static JsonObjectRequest mJsonObjectRequest;    
protected static boolean busy = true;

public static JSONObject makeJsonObjectRequest(Context context, int method, String url){
    final JSONObject[] jsonObject = new JSONObject[1];
    mJsonObjectRequest = new JsonObjectRequest
            (method, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    jsonObject[0] = response;
                    busy = false;
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    try {
                        jsonObject[0] = new JSONObject(error.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    busy = false;
                }
            });

    // Access the RequestQueue through your singleton class.
    VolleySingleton.getInstance(context).addToRequestQueue(mJsonObjectRequest);

    while (true) {
         if (!busy) break;
    }
    return jsonObject[0];
}
}

MainActivity.java:

JSONObject jsonObject = Utils.makeJsonObjectRequest(this, Request.Method.GET, url);
mTxtDisplay.setText("Response: " + jsonObject.toString());

অ্যাপ্লিকেশন চললে, jsonObject সর্বদা শূন্য হয়। আমি জিজ্ঞাসা করতে চাই যে আমি যদি জেসনঅবজেক্ট [0] এর ভিতরে জেসনঅবজেটরেখেসে অন রিসপন্স না বলা পর্যন্ত ফিরতে বিলম্ব করতে পারি কিনা। আমি কি করতে পারি এবং কীভাবে? ।


@ জেনাদাদি সাপ্রিনকিন এবং @ গোপাল সিং সিরভির উত্তরগুলির জন্য ধন্যবাদ, আমি কলযোগ্য এবং ইন্টারফেস শ্রোতা উভয় উপায়ে চেষ্টা করেছি। উভয়ই কাজ করেছেন, তবে আমি শ্রোতার পথে পছন্দ করি।

OnRequestCompletedListener.java:

package com.example.volleyapp;

import org.json.JSONObject;

public interface OnRequestCompletedListener {
    void onRequestCompleted(JSONObject response);
}

Utils.java:

package com.example.volleyapp;

import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.Callable;

public class Utils {
    static JsonObjectRequest mJsonObjectRequest;
    static Callable<Void> func=null;

    public static void makeJsonObjectRequest_Listener(final Context context, final int method, final String url, final OnRequestCompletedListener listener){
        mJsonObjectRequest = new JsonObjectRequest
                (method, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        listener.onRequestCompleted(response);
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        try {
                            listener.onRequestCompleted(new JSONObject(error.toString()));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

        // Access the RequestQueue through your singleton class.
        VolleySingleton.getInstance(context).addToRequestQueue(mJsonObjectRequest);
    }

    public static void makeJsonObjectRequest_Callable(final Context context, final int method, final String url, final MainActivity.JsonCallable doRequest){
        mJsonObjectRequest = new JsonObjectRequest
                (method, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try{
                            func = doRequest;
                            doRequest.jsonObject = response;
                            func.call();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        try {
                            func = doRequest;
                            doRequest.jsonObject = new JSONObject(error.toString());                                             
                            func.call();                                
                            } catch (JSONException e) {
                                e.printStackTrace();
                        }
                    }
                });

        // Access the RequestQueue through your singleton class.
        VolleySingleton.getInstance(context).addToRequestQueue(mJsonObjectRequest);
    }
}

MainActivity.java:

package com.example.volleyapp;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONObject;

import java.util.concurrent.Callable;


public class MainActivity extends Activity {
    JsonCallable jsonCallable=null;
    TextView mTxtDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTxtDisplay = (TextView) findViewById(R.id.textView);
        String url = "https://10.0.2.2/aspnet/webapi/products/1";

        // Callable way
        // jsonCallable = new JsonCallable();           
        // Utils.makeJsonObjectRequest_Callable(this, Request.Method.GET, url, jsonCallable);

        // Listener way
        OnRequestCompletedListener listener = new OnRequestCompletedListener() {
            @Override
            public void onRequestCompleted(JSONObject response) {
                mTxtDisplay.setText(response.toString());
            }
        };
        Utils.makeJsonObjectRequest_Listener(this, Request.Method.GET, url, listener);          
    }      

    class JsonCallable implements Callable {

        public JSONObject jsonObject;

        public JsonCallable(){
        }

        @Override
        public Object call() throws Exception {
            mTxtDisplay.setText(jsonObject.toString());
            return null;
        }
    }
}

আবারও, আপনার সহায়তার জন্য সবাইকে ধন্যবাদ!


আমি এটি তৈরি করেছি এবং নিম্নলিখিত পদ্ধতির মতো পরিচালনা করেছি দয়া করে একবার দেখুন, আশা করি এটি আপনার পক্ষে কার্যকর হবে

public class APIManager {



    public static void createRequest(Context c, String requestTag,
            String endPoint, List<NameValuePair> params,
            final OnRequestCompletedListener listener,
            TransParentProgressDialog pd) {
        ServerDetails serverDetails = new ServerDetails(c, endPoint, params);
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                serverDetails.getQueryUrl(), null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        listener.onRequestCompleted(response);
                    }
                }, getErrorListener(c, pd)) {

        };
        AppController.getInstance().addToRequestQueue(request, requestTag);
    }

public static ErrorListener getErrorListener(final Context c,
            final TransParentProgressDialog pd, final TextView tvEmpty,
            final String errorText) {

        Response.ErrorListener listener = new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (pd != null && pd.isShowing()) {
                    pd.dismiss();
                }
                if (tvEmpty != null) {
                    tvEmpty.setText(errorText);
                }
                MyDialog dialog;
                Log.d("volley-error", error.toString());

                if (error instanceof TimeoutError) {
                    dialog = new MyDialog(c, "Server Timeout");
                    dialog.show();
                    return;
                } else if (error instanceof NoConnectionError) {
                    dialog = new MyDialog(c, "No Connection or Invalid Url");
                    dialog.show();
                    return;

                } else if (error instanceof ServerError) {
                    NetworkResponse response = error.networkResponse;
                    if (response != null) {
                        // int statusCode = response.statusCode;
                        byte[] data = response.data;
                        if (data != null) {
                            String str = new String(data);
                            try {
                                JSONObject object = new JSONObject(str);
                                Log.d("error response", object.toString());
                                if (object.has("errors")) {
                                    JSONArray errors = object
                                            .getJSONArray("errors");
                                    JSONObject errorObject = errors
                                            .getJSONObject(0);
                                    dialog = new MyDialog(c, "Error!",
                                            errorObject.getString("message"));
                                    dialog.show();
                                } else {
                                    dialog = new MyDialog(c, "Error!",
                                            object.toString());
                                    dialog.show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                dialog = new MyDialog(c, "Error!", "Error");
                                dialog.show();
                            }
                        } else {
                            dialog = new MyDialog(c, "Server Error");
                            dialog.show();
                        }
                    } else {
                        dialog = new MyDialog(c, "Server Error");
                        dialog.show();
                    }

                } else if (error instanceof NetworkError) {
                    NetworkResponse response = error.networkResponse;
                    if (response != null) {
                        // int statusCode = response.statusCode;
                        byte[] data = response.data;
                        if (data != null) {
                            String str = new String(data);
                            try {
                                JSONObject object = new JSONObject(str);
                                Log.d("error response", object.toString());
                                if (object.has("errors")) {
                                    JSONArray errors = object
                                            .getJSONArray("errors");
                                    JSONObject errorObject = errors
                                            .getJSONObject(0);
                                    dialog = new MyDialog(c, "Error!",
                                            errorObject.getString("message"));
                                    dialog.show();
                                } else {
                                    dialog = new MyDialog(c, "Error!",
                                            object.toString());
                                    dialog.show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                dialog = new MyDialog(c, "Error!", "Error");
                                dialog.show();
                            }
                        } else {
                            dialog = new MyDialog(c, "Network Error");
                            dialog.show();
                        }
                    } else {
                        dialog = new MyDialog(c, "Network Error");
                        dialog.show();
                    }
                } else if (error instanceof ParseError) {
                    dialog = new MyDialog(c, "Parse Error");
                    dialog.show();
                } else if (error instanceof AuthFailureError) {
                    NetworkResponse response = error.networkResponse;
                    if (response != null) {
                        // int statusCode = response.statusCode;
                        byte[] data = response.data;
                        if (data != null) {
                            String str = new String(data);
                            try {
                                JSONObject object = new JSONObject(str);
                                Log.d("error response", object.toString());
                                if (object.has("errors")) {
                                    JSONArray errors = object
                                            .getJSONArray("errors");
                                    JSONObject errorObject = errors
                                            .getJSONObject(0);
                                    dialog = new MyDialog(c, "Error!",
                                            errorObject.getString("message"));
                                    dialog.show();
                                } else {
                                    dialog = new MyDialog(c, "Error!",
                                            object.toString());
                                    dialog.show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                dialog = new MyDialog(c, "Error!", "Error");
                                dialog.show();
                            }
                        } else {
                            dialog = new MyDialog(c, "Error!", "Error");
                            dialog.show();
                        }
                    } else {
                        dialog = new MyDialog(c, "Error connecting server");
                        dialog.show();
                    }
                }
            }
        };
        return listener;
    }
}

এবং অনুরোধটির জন্য কল ব্যাক করার জন্য ইন্টারফেসটি শেষ

public interface OnRequestCompletedListener {

        public void onRequestCompleted(JSONObject response);
    }




android-volley