doGet()servlet中带有参数的多个if else语句

时间:2014-11-12 10:54:24

标签: java android servlets android-asynctask

尝试在servlet doGet()中执行不同的方法时遇到了一些问题。因此,当我点击按钮时,它将传递eventID:

viewDtlEventBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent eventDtlIntent = new Intent(context, EventDetail.class);
            eventDtlIntent.putExtra("eventID", eventIDTV.getText());
            startActivity(eventDtlIntent);
        }
    });

然后在我的EventDetail类中,我正在AsyncTask类中执行该方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_detail);
    context = this; 
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        eventID = extras.getString("eventID");
    }       
    eventModel.setEventID(eventID);
    new GetEventDetailAsyncTask(context).execute(eventModel);
}

在我的AsyncTask类中,我在我的控制器中调用该方法,该方法检索从servlet返回的JSON:

@Override
protected Double doInBackground(Event... params) {
    try {
        eventCtrl.getEventDetailByID(params[0]);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

在我的控制器类中:

public Event getEventDetailByID(Event event) throws JSONException {
    Event eventModel = new Event();
    String page;
    JSONArray jsonArray;
    String eventID = event.getEventID();

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(ENeighbourhoodActivity.URL
                + "?getEventDetailByID&eventID=" + eventID +"");
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        page = "{\'EventDetail\':" + responseString + "}";
        try {
            JSONObject jsonObject = new JSONObject(page);
            jsonArray = jsonObject.getJSONArray("EventDetail");
            int length = jsonArray.length();
            for (int i = 0; i < length; i++) {
                JSONObject attribute = jsonArray.getJSONObject(i);
                String eventName = attribute.getString("eventName");
                eventModel.setEventName(eventName);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return event;
}

基本上从这里开始,我正在访问servlet。在我的doGet()中的serlvet类:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    JSONArray jsonArray = new JSONArray();
    PrintWriter out = response.getWriter();

    if (request.getParameter("getAllEvent") != null) {  
    }

    catch (JSONException je) {
        System.out.println(je.getMessage());
    } catch (Exception exc) {
        System.out.println(exc.getMessage());
    }
    out.println(jsonArray.toString());
    }
}

我已经为其他一些方法提供了一个if语句。我想知道如何将getEventDetailByID和eventID参数传递给servlet,以便它知道要运行哪个方法。

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下是如何获取eventID值的示例。

        //gets value from getEventDetailByID parameter.
        String detail = request.getParameter("getEventDetailByID");
        if (detail != null && !detail.equals("")) {
            int eventId = Integer.parseInt(request.getParameter("eventID"));
            //get event detail by id with id 
            //e.g resultObject = myMethod(detail, eventId);
        }

更新1: 做出所需要的更好方法是:

        String action = request.getParameter("action");
        if (action.equalsIgnoreCase("GetById")) {
            int eventId = Integer.parseInt(request.getParameter("eventID"));
            //get event detail by id with id 
            //e.g resultObject = getById(eventId);
        } else if (action.equalsIgnoreCase("GetAllEvents")) {
            //Get all events
            //e.g resultObject = GetAllEvents();
        } else {
        }

网址使用情况:

通过id获取事件:

http://localhost:8080/WebService/EventDetailServlet?action=GetById&eventID=46

获取所有活动详情

http://localhost:8080/WebService/EventDetailServlet?action=GetAllEvents