【问题标题】:Know if a method of a Class ends知道一个类的方法是否结束
【发布时间】:2018-07-19 23:11:48
【问题描述】:

我有一个类,它给我一个数据库字符串。 我在我的活动中调用类,然后使用该字符串。 因为我通过 Log.d 得到了它,所以返回了搅拌,但是我的 Activity 的字符串变量没有得到它,因为程序继续没有等待类结束。 (对不起我的英语不好)

代码: 活动:

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;
        }
    }

【问题讨论】:

  • 这里似乎使用了并发和使用回调方法(观察者模式)的概念,因为在 Cargar_Imagen(...) 方法结束时不会分配 sBitmap 字符串,因为它在回调方法中的不同线程中分配。

标签: java android class


【解决方案1】:

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);

【讨论】:

  • 在活动中我必须做什么?
  • 如果你从activity调用Cargar_Imagen,在activity中实现接口,并将this传递给新参数。那应该做的工作。我用一个例子更新了我的答案
  • 它说:“非静态方法 onFinish 不能从静态上下文中引用:/ 如果将其设为静态,则会出现 lenguaje level1.7 的问题:“语言级别 1.7 不支持静态接口方法调用"
  • 你确定你叫它callback.onFinish而不是CallBack.onFinish
  • 对不起,我没有看到最终的回调回调:/
【解决方案2】:

您已经写了retrun sBitmap;,请尝试使用正确的return,如果它解决了问题,请告诉我们。

【讨论】:

  • 对不起,我在这里写得不好,我使用返回,应用程序在 movil 中运行良好,但不要这样做。
猜你喜欢
  • 2014-09-20
  • 2012-10-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2017-01-13
相关资源
最近更新 更多