知道类的方法是否结束

时间:2018-07-19 23:11:48

标签: java android class

我有一个类,可以获取数据库的字符串。 我在“活动”中调用类,然后使用该字符串。 该stngng返回是因为我是通过Log.d获得的,但是Activity的String变量却没有得到它,因为该程序无需等待类就可以继续运行。 (对不起,我的英语不好)

代码: 活动:

Cargar_Imagen ci = new Cargar_Imagen();
String s = ci.Cargar_Imagen(params...);

班级:

    public class Cargar_Imagen {
        private Map<String, String> params;
        String sBitmap = "";
        public String Cargar_Imagen(final String email, final String id, final String f1, final Context context, final String type, final View v){
            StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://feticher.webcindario.com/cargar_imagen.php",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            Log.d("Imagen", "Se cargo la foto");
                            try {
                                JSONObject jsonRespose = new JSONObject(s);
                                sBitmap =jsonRespose.getString("imagen");
                                Log.d("Imagen", "STRING: " + sBitmap);

                                Download_Image download_image = new Download_Image();
                                download_image.Download_Image(f1, type,context, v, sBitmap);
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Log.d("Imagen", "ERROR: " + e.getMessage());
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("Imagen", "No se pudo cargar la foto");
                        }
                    }) {
                @Override
                protected Map<String, String> getParams(){
                    Log.d("Imagen", "Ejecutando MAP Cargar Imagen");
                    Log.d("Imagen", "DATOS: " + id + " " + email + " " + f1);
                    Map<String, String> params = new Hashtable<String, String>();
                    params.put("id", id);
                    params.put("email", email);
                    params.put("foto", f1);
                    Log.d("Imagen", "PASO POR AKI");
                    return params;
                }};

            RequestQueue queue = Volley.newRequestQueue(context);
            queue.add(stringRequest);
            return sBitmap;
        }
    }

2 个答案:

答案 0 :(得分:0)

您已写过retrun sBitmap;,请尝试使用正确的return,并让我们知道它是否解决了问题。

答案 1 :(得分:0)

sBitmap是在方法执行后分配的,因为它是异步的。一种方法是用方法[例如onFinish(String)]声明一个接口,并将其作为Cargar_Imagen的参数传递,然后您可以在onResponse.Listener方法内调用它。

// The interface
interface CallBack {
    public void onFinish(String response);
}

// The activity (which calls Cargar_Imagen) should implement CallBack
public class MainActivity extends Activity implements CallBack

    public void onFinish(String response) {
        // here the method did finish, do something with the string
    }

    // somewhere you call to Cargar_Imagen
    String s = ci.Cargar_Imagen(params..., this);
}

// the method signature should receive a CallBack
public String Cargar_Imagen(final String email, final String id, final String f1, final Context context, final String type, final View v, final CallBack callback){
    /* code .... */

    // ...
    // in your onResponse
    callback.onFinish(sBitmap);