2016-03-25 17 views
2

ボレーの値を投稿しようとしています。ボタンをクリックすると、ブール値がtrueに設定されます。ブール値は、 mysqlデータベース。キューにリクエストを追加しようとするとエラーが発生します。何が間違っているのですか?これは、あなたがないPOST要求を行っている私のコードボレーを使用してブール値を投稿する方法

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

      } 


     }); 

SINGLETON

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

ERROR

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

volleyのシングルトンクラスを作成しましたか? –

+0

ブール事が正しいです、あなたはボレーのシングルトンクラスを逃す必要があります。 –

+0

次の 'JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST、url、obj、new Response.Listener (){...'のように、 'GET'の代わりに' POST'を使うべきです) – BNK

答えて

2

です。

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 Singletonに追加する必要があります。 Reference

+0

"content-type"と "application/json"の代わりに何を使うべきですか? charset = utf-8 "?@RohitArya –

+0

jsonオブジェクトリクエストを作成しているので、同じものを使用する必要がありますが、バックエンドの実装に依存します。 –

関連する問題