我的SearchActivity.java看起来像这样:
public class SearchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
final EditText editText = (EditText) findViewById(R.id.searchView);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
String query = editText.getText().toString();
doMySearch(query);
handled = true;
}
return handled;
}
});
}
public void doMySearch(String query) {
System.out.println("print if doMySearch was opened");
System.out.println(query);
List<Show> subscribedShowsList = new ArrayList<Show>();
String url = "http://api.tvmaze.com/search/shows?q=";
String final_url = url + query;
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, final_url, (String)null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
System.out.println("print if we got to onResponse");
String resss = response.toString();
System.out.println(resss);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
System.out.println("this is request" + request);
}
}
控制台输出是:
05-04 19:46:39.054 29615-29615/com.example.user.test I/System.out: print if doMySearch was opened
05-04 19:46:39.054 29615-29615/com.example.user.test I/System.out: girls
05-04 19:46:39.054 29615-29615/com.example.user.test I/System.out: this is request[ ] http://api.tvmaze.com/search/shows?q=girls 0xfc1517a8 NORMAL null
网址是正确的,但正如您所见,onResponse甚至没有运行。我已经尝试了JSONArrayRequest和JSONObjectRequest,他们(不要)也一样。我不知道这里有什么不妥。有什么建议吗?
答案 0 :(得分:1)
您需要RequestQueue
来发起排球请求。您所做的只是创建一个匿名类,在创建它时不会执行onResponse
。
JsonArrayRequest request = new JsonArrayRequest(...);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);