我正在使用这个类,在引用了几个google搜索和stackoverflow问题之后,我使用POST方法获取json数组,但是我的代码给出了一个奇怪的错误Cannot Resolve Symbol
POST
这是我的活动
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));
}
}
}
我正在关注androidhive的教程
请建议在哪里犯错误
答案 0 :(得分:1)
我这样做了(请参阅示例代码),确保必须在您的活动类中导入import com.android.volley.Request;
,否则Request.Method.POST
将无效,无法解析将出现
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);
并使用此课程
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));
}
}
}
答案 1 :(得分:0)
检查导入Method
。它应该是
import com.android.volley.Request.Method
可能是您已导入
import java.lang.reflect.Method;
答案 2 :(得分:0)
使用
JsonArrayRequest movieReq = new PostJsonArrayRequest(Request.Method.POST,url,
new Response.Listener<JSONArray>() {
并确保导入,
import com.android.volley.Request.Method;
希望它能奏效。