Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8392811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:37:04+00:00 2026-06-09T19:37:04+00:00

HI I am teaching myself android development.To start with, I have implemented a webserver

  • 0

HI I am teaching myself android development.To start with, I have implemented a webserver on android using apache http utils, which serves a login page when browsed to localhost:8080 on emulator.

I would like to know how can I parse the login request on android server side .This web server serves content on the fly based on the Uri requested.Say when the login page is submitted the form is posted to /login.do, on the localhost:8080.

I have registered a handler for that action as shown below.I was wondering if there is a way to parse the request body in my handler and serve the html accordingly.I have only one field in the form to parse namely password.I could get only as far as reading the request headers.

Any ideas or pointers in the right direction would be greatly appreciated.Thanks

import java.io.BufferedInputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.io.OutputStreamWriter;  

import org.apache.http.HeaderElement;  
import org.apache.http.HeaderElementIterator;  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpException;  
import org.apache.http.HttpRequest;  
import org.apache.http.HttpResponse;  
import org.apache.http.NameValuePair;  
import org.apache.http.entity.ContentProducer;  
import org.apache.http.entity.EntityTemplate;  
import org.apache.http.message.BasicHeaderElementIterator;  
import org.apache.http.protocol.HttpContext;  
import org.apache.http.protocol.HttpRequestHandler;  

import android.content.Context;  

public class LoginHandler implements HttpRequestHandler {  
    private Context context = null;  
    String req;  
    public LoginHandler(Context context) {  
        this.context = context;  
    }  

    @Override  
    public void handle( final HttpRequest request, HttpResponse response,  
            HttpContext httpcontext) throws HttpException, IOException {  
        HttpEntity entity = new EntityTemplate(new ContentProducer() {  
            public void writeTo(final OutputStream outstream ) throws IOException {  
                String resp;  

            OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");     
            HeaderIterator it = request.headerIterator();
            while (it.hasNext())
            {
                System.out.println(it.next());
            }
                if (validateUser()==true)  
                    resp = "<html><head></head><body><h1>Home<h1><p>Success."+req+"</p></body></html>";  

                else{  
                    resp="<html><head></head><body><h1>Home<h1><p>Login Failed.</p></body></html>";}  
                writer.write(resp);  
                writer.flush();  
            }  


        });  
        response.setHeader("Content-Type", "text/html");  
        response.setEntity(entity);  

    }  
    boolean validateUser(){  
        boolean valid=false;  
        //if valid  valid=true else valid=false  
        return valid;  
    }  



}  

The output is as follows:

    08-15 15:54:22.112: I/Process(650): Sending signal. PID: 650 SIG: 9
08-15 15:54:27.532: I/HTTPSERVICE(662): Creating and starting httpService
08-15 15:54:27.542: I/Notification(662): Notification Shown
08-15 16:08:19.332: I/HTTPSERVICE(662): Destroying httpService
08-15 16:08:19.352: I/Notification(662): Notification Destroyed
08-15 16:20:14.852: I/HTTPSERVICE(690): Creating and starting httpService
08-15 16:20:14.862: I/Notification(690): Notification Shown
08-15 16:20:28.102: D/dalvikvm(690): GC freed 2292 objects / 135976 bytes in 157ms
08-15 16:20:28.872: I/Resources(690): Loaded time zone names for en_US in 3090ms.
08-15 16:20:28.882: I/global(690): Default buffer size used in BufferedReader constructor.     It would be better to be explicit if an 8k-char buffer is required.
08-15 16:20:35.302: I/System.out(690): Host: localhost:8080
08-15 16:20:35.382: I/System.out(690): Accept-Encoding: gzip
08-15 16:20:35.422: I/System.out(690): Referer: http://localhost:8080/
08-15 16:20:35.422: I/System.out(690): Accept-Language: en-US
08-15 16:20:35.442: I/System.out(690): User-Agent: Mozilla/5.0 (Linux; U; Android  2.1-update1; en-us; sdk Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17
08-15 16:20:35.472: I/System.out(690): Origin: http://localhost:8080
08-15 16:20:35.512: I/System.out(690): Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
08-15 16:20:35.533: I/System.out(690): Content-Type: application/x-www-form-urlencoded
08-15 16:20:35.562: I/System.out(690): Accept-Charset: utf-8, iso-8859-1,utf-16,*;q=0.7
08-15 16:20:35.599: I/System.out(690): Content-Length: 25

I could not find any resource applicable to my case on the apache site.Is is possible to read the requestbody directly from the url?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:37:06+00:00Added an answer on June 9, 2026 at 7:37 pm

    After looking around for ages I found the solution. Adding the following in the handle method does the trick.Thanks to the original poster.http://www.androiddevblog.net/android/a-bare-minimum-web-server-for-android-platform

                if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity != null) {
        Log.v("RequestBody", EntityUtils.toString(entity, "UTF-8"));
        entity.consumeContent();
        }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to programming in general and have been teaching myself using Android
I have started teaching myself about the Android NDK and I have followed this
I am teaching myself some Python and I have come across a problem which
Teaching myself some c# and my problem is that I have a class which
I'm currently teaching myself Objective-C and Iphone Development using the very good 'Begining IPhone
I am teaching myself Android using Eclipse, the Android plug-in, and Sams Teach Yourself
I am teaching myself asp .net mvc3. I have a partial view which uses
I'm teaching myself Rails through PragProg's (apparently outdated - I'm using Rails 3.2.3) Rails
I have recently started teaching myself C# and Asp.net. I am trying to build
I am teaching myself to use JavaCC in a hobby project, and have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.