在loopj中如何获取值来传递get请求的url

时间:2016-09-18 14:18:08

标签: android loopj

我得到了使用loopj从使用此代码获取其余服务的所有员工的列表。

public class MainActivity extends ActionBarActivity {
private ListView employeeList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getEmployees();
}
    private void getEmployees() {
         List<Header> headers = new ArrayList<Header>();
         headers.add(new BasicHeader("Accept", "application/json"));

         EmployeeRestClient.get(MainActivity.this, "RestExample/employee", 
        headers.toArray(new Header[headers.size()]),null, 
            new JsonHttpResponseHandler() {
         @Override
         public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
         ArrayList<Employee> employeeArray = new ArrayList<Employee>();
         EmployeeAdapter employeeAdapter = new EmployeeAdapter(MainActivity.this, employeeArray);

           for (int i = 0; i < response.length(); i++) {
            try {
                  employeeAdapter.add(new Employee(response.getJSONObject(i)));
                 } catch (JSONException e) {
                 e.printStackTrace();
                   }
               }

           employeeList = (ListView) findViewById(R.id.list_employees);
          employeeList.setAdapter(employeeAdapter);
             }
        });
     }

我希望获得员工列表,select * from employees where age = ? 因此,在getEmployees(Long age)方法中,如何通过RestExample/employee/{age}

等网址传递年龄值

1 个答案:

答案 0 :(得分:1)

this内初始化ListViewemployeeList),数据源(employeeArray)和适配器(employeeAdapter),而不是onCreate中的onSuccess方法。

employeeList = (ListView) findViewById(R.id.list_employees);
employeeArray = new ArrayList<Employee>();
employeeAdapter = new EmployeeAdapter(MainActivity.this, employeeArray);
employeeList.setAdapter(employeeAdapter);

您的getEmployees方法,

private void getEmployees(Long age) {
    List<Header> headers = new ArrayList<Header>();
    headers.add(new BasicHeader("Accept", "application/json"));

    EmployeeRestClient.get(MainActivity.this, "RestExample/employee/" + age, 
        headers.toArray(new Header[headers.size()]), null, 
        new JsonHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
            for (int i = 0; i < response.length(); i++) {
                try {
                  employeeArray.add(new Employee(response.getJSONObject(i)));
                } catch (JSONException e) {
                 e.printStackTrace();
                }
            }
            // notify the adapter that the data has changed
            employeeAdapter.notifyDataSetChanged();
         }

    });
}