java - 如何使用 volley 发布 boolean 值

标签 java android android-volley

我正在尝试使用 volley 库发布 boolean 值。单击按钮时, boolean 值将设置为 true。然后将该 boolean 值发布到我的 php 脚本以增加 mysql 数据库中的计数器。我在尝试将请求添加到队列时遇到错误。我究竟做错了什么?这是我的代码

        holder.custom_button.setOnClickListener(new View.OnClickListener() {
            int count;


            @Override
            public void onClick(View v) {

                count = 0;

                superHeroes.get(position).setCount(superHeroes.get(position).getCount() + 1);
                holder.txtCount.setText(superHeroes.get(position).getCount() + "");


                final String url = "http://10.0.2.2/likes.php";

                isLiked = true;

                JSONObject obj = new JSONObject();

                try {
                    obj.put("isLiked", true);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JsonObjectRequest jsObjRequest = new JsonObjectRequest

                        (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {


                            @Override
                            public void onResponse(JSONObject response) {

                            }
                        }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // TODO Auto-generated method stub

                            }
                        });

// Access the RequestQueue through your singleton class.
// i am getting error here
                customVolleyRequest.getInstance(this).addToRequestQueue(jsObjRequest);

            }


        });

单例

package net.simplifiedcoding.myfeed;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageLoader;
/**
 * Created by Belal on 12/5/2015.
 */

public class CustomVolleyRequest {

    private static CustomVolleyRequest customVolleyRequest;
    private static Context context;
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;

    private CustomVolleyRequest(Context context) {
        this.context = context;
        this.requestQueue = getRequestQueue();

        imageLoader = new ImageLoader(requestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap>
                            cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

    public static synchronized CustomVolleyRequest getInstance(Context context) {
        if (customVolleyRequest == null) {
            customVolleyRequest = new CustomVolleyRequest(context);
        }
        return customVolleyRequest;
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
            Network network = new BasicNetwork(new HurlStack());
            requestQueue = new RequestQueue(cache, network);
            requestQueue.start();
        }
        return requestQueue;
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }
}

错误

'getInstance(android.content.Context)' in 'net.simplifiedcoding.myfeed.CustomVolleyRequest' cannot be applied to '(anonymous android.view.View.OnClickListener)'

最佳答案

没有发出POST请求。你应该这样做:

JsonObjectRequest req = new JsonObjectRequest( Request.Method.POST, url,
      new JSONObject(jsonParams),
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // Handle response
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
              //   Handle Error
            }
        }) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }
};
AppController.getInstance().addToRequestQueue(req); // AppController is Volley Singleton.

您使用过CardAdapter,我不知道那是什么。您应该始终将请求对象添加到Volley SingletonReference .

关于java - 如何使用 volley 发布 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36214650/

相关文章:

java - 新的 Google Map Api for Java 应用程序,在 JPanel 中添加 map

java - Jarsigner(MIME 内容类型不是应用程序/时间戳回复)

java - 如何在不同的代码行中使用 int 并增加其值

android - Firebase 仪表板不显示顶级产品的价格

java - 项目目标 (Android 5.0.1) 未正确加载

android - 如何运行 Volley 测试?

android - 如何在 libgdx 中使用网络框架(volley、okhttp 等)?

java - 如何使用 WinRun4J 将 Java 应用程序作为 Windows 服务运行

android - 为什么 BroadcastReceiver 在使用 MediaPlayer 时会崩溃?

android - 如何从 Volley 中隐藏默认的 "not available"网页