2012-08-13 2 views
2

안녕하세요. 저는 Android 개발을 가르치고 있습니다. 시작하려면 에뮬레이터에서 localhost:8080으로 찾아 볼 때 로그인 페이지를 제공하는 apache http utils를 사용하여 Android에서 웹 서버를 구현했습니다.http 구성 요소를 사용하여 http 요청 서버 측을 구문 분석합니다.

나는 어떻게 안드로이드 서버 측에서 로그인 요청을 구문 분석 할 수 있는지 알고 싶습니다.이 웹 서버는 Uri가 요청한 내용을 기반으로 즉석에서 콘텐츠를 제공합니다. 로그인 페이지가 전송되면 양식이/login에 게시됩니다. do, localhost:8080에 있습니다.

아래와 같이 해당 작업에 대한 처리기를 등록했습니다. 처리기에서 요청 본문을 구문 분석하고 이에 따라 html을 처리하는 방법이 있는지 궁금합니다. 양식에 단 하나의 필드 즉, password 요청 헤더를 읽는 것만 가능합니다.

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 

내가 내 경우에 적용되는 리소스를 찾을 수 없습니다 : 올바른 방향으로

어떤 아이디어 또는 포인터를 크게 출력은 다음과

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; 
    } 



} 

appreciated.Thanks 것 아파치 site.Is는 직접 url에서 requestbody을 읽을 수 있습니까?

답변

0

연령대를 살펴본 후에 해결책을 찾았습니다. handle 메서드에 다음을 추가하면 트릭이 수행됩니다. 원래 포스터를 사용해 주셔서 감사합니다. 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(); 
    } 
    }