Android从servlet获取请求/属性

时间:2015-11-26 09:16:27

标签: android servlets attributes

我有现有的servlet,我正在使用android连接它。我想知道android能够在调用后从servlet获取请求信息吗?如果无法检索,是否有其他方法可以检索,比如创建新的servlet(这是我最不喜欢的选择)。我从servlet返回的结果是 listing-details.jsp 信息。

我想要获得的请求是 trans 列表

的信息
request.setAttribute("transactions",trans);

servlet

protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    String sort;
    String propertyID= request.getParameter("pid");
    Property result = PropertyManager.getInstance().searchPropertyByID(propertyID);
    sort = request.getParameter("transort");
    String role = "";
    List<Transaction> trans = TransactionManager.getInstance().searchTransactionByName(result.getProperty_district(),result.getProperty_name(),sort);

    RequestDispatcher requestDispatcher = request
    .getRequestDispatcher("listing-details.jsp");
    request.setAttribute("property", result);
    request.getSession().removeAttribute("property");
    request.getSession().setAttribute("property",result);
    request.setAttribute("transactions",trans);
    requestDispatcher.forward(request, response);
    return;
}

这是我的Android活动和网址

URL : http://xxxx/ListingDetailServlet

 private class GetListingDetail extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String output = null;
        for (String url : urls) {
            output = getOutputFromUrl(url);
        }
        return output;
    }

    public String getOutputFromUrl(String url) {
        StringBuffer output = new StringBuffer("");
        //Log.i(TAG, "BEfore input stream ");
        try {
            InputStream stream = getHttpConnection(url);
            BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
            String s = "";
            while ((s = buffer.readLine()) != null)
                output.append(s);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return output.toString();
    }

    // Makes HttpURLConnection and returns InputStream
    public InputStream getHttpConnection(String urlString)
            throws IOException {
        Log.i(TAG, "Inside inputstream ");
        InputStream stream = null;
        urlString = urlString + "?pid=pid624";//&transort=";
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);

        try {

            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("POST");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream(); 
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }

    @Override
    protected void onPostExecute(String output) {
        Log.i(TAG, output);
    }

0 个答案:

没有答案