1

I am having this class where am fetching json array with POST method after referring several google searches and stackoverflow question but my code gives an weird error Cannot Resolve Symbol POST

here is my activity

public class fbuk extends Activity {

    // Movies json url
    private static final String url = "http://example.com/xyz.php";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;

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

        listView = (ListView) findViewById(R.id.listview);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // changing action bar color

        // Creating volley request obj
        JsonArrayRequest movieReq = new PostJsonArrayRequest(Method.POST, url, null, 
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Movie movie = new Movie();
                            movie.setTitle(obj.getString("ttl"));
                            movie.setThumbnailUrl(obj.getString("img"));
                            movie.setRating(obj.getString("stts"));
                            movie.setYear(obj.getString("rel"));

                            movieList.add(movie);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, 
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();
                }
            });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }
}


public class PostJsonArrayRequest extends JsonRequest<JSONArray> {

    /**
     * Creates a new request.
     * @param url URL to fetch the JSON from
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public PostJsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
        super(Method.POST, url, null, listener, errorListener);
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("q", "ram");
        return params;
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                    new String(response.data,   HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}

I am following a tutorial from androidhive

Please suggest where am making the mistake

Mykola Yashchenko
  • 5,103
  • 3
  • 39
  • 48
user3225075
  • 373
  • 1
  • 5
  • 23

3 Answers3

1

I have done like this (see sample code), make sure that import com.android.volley.Request; must be imported in your activity class otherwise Request.Method.POST will not work and Unable to Resolve will appear

String url = "http://example.com/xyz.php";

    ErrorListener errorListener = new ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError errorResponce) 
        {

        }
    };

    Listener<JSONArray > jsonArrayListener = new Listener<JSONArray>() 
    {
        @Override
        public void onResponse(JSONArray response) {
            // TODO Auto-generated method stub

        }
    };


    PostJsonArrayRequest req = new PostJsonArrayRequest(Request.Method.POST, url , null, jsonArrayListener, errorListener);

And use this class for

 public class PostJsonArrayRequest extends JsonRequest<JSONArray> 
{

    public PostJsonArrayRequest(int method, String url, String requestBody,
            Listener<JSONArray> listener, ErrorListener errorListener) 
    {
        super(method, url, requestBody, listener, errorListener);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) 
    {
        try {
            String jsonString =
                    new String(response.data,   HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

}
kashif181
  • 315
  • 2
  • 11
  • Error:(90, 37) error: constructor PostJsonArrayRequest in class fbuk.PostJsonArrayRequest cannot be applied to given types; required: String,Listener,ErrorListener found: int,String,>, reason: actual and formal argument lists differ in length – user3225075 Apr 03 '15 at 05:48
  • I have edited the answer and add PostJsonArrayRequest class as well now check hopefully it will work. – kashif181 Apr 03 '15 at 06:40
  • your answer took me half way but am unable to send the params. how to do that? – user3225075 Apr 03 '15 at 22:02
  • please help me on this – user3225075 Apr 04 '15 at 08:08
  • you can override getParams method in PostJsonArrayRequest class, try this otherwise do tell me if issue not get resolve – kashif181 Apr 05 '15 at 07:43
0

Check your import for Method. It should be

import com.android.volley.Request.Method

May be you have imported as a

import java.lang.reflect.Method;
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Error:(90, 37) error: constructor PostJsonArrayRequest in class fbuk.PostJsonArrayRequest cannot be applied to given types; required: String,Listener,ErrorListener found: int,String,>, reason: actual and formal argument lists differ in length – user3225075 Apr 03 '15 at 05:45
  • You should extends your `PostJSONArrayRequest` class with `JsonArrayRequest` instead of `JsonRequest` – Piyush Apr 03 '15 at 05:47
  • now i got error near super(Method.POST, url, null, listener, errorListener); cannot resolve error – user3225075 Apr 03 '15 at 05:50
  • You can simply use `JsonArrayRequest movieReq = new JsonArrayRequest` – Piyush Apr 03 '15 at 05:51
  • how to send params then? – user3225075 Apr 03 '15 at 05:58
  • The format is same you just need to change `PostJSONArrayRequest` to `JSONArrayRequest`. – Piyush Apr 03 '15 at 05:58
  • @Override protected Map getParams() throws AuthFailureError { HashMap params = new HashMap(); params.put("name", "value"); return params; } where shall i put these line of codes? – user3225075 Apr 03 '15 at 06:03
0

Use

JsonArrayRequest movieReq = new PostJsonArrayRequest(Request.Method.POST,url,
        new Response.Listener<JSONArray>() {

and make sure to import,

import com.android.volley.Request.Method;

hope it will work.

Piyush
  • 18,895
  • 5
  • 32
  • 63
sandeep gupta
  • 301
  • 1
  • 3
  • 14