2012-08-08 84 views
1

我正在一個Android web服務器上工作。當我在模擬器瀏覽器上轉到localhost:8080時,它提供了一個帶有密碼字段的頁面/表單。在成功驗證密碼後,我想將用戶重定向到成功/失敗頁面。讀取傳入的http post請求並解析密碼字段進行驗證的最佳方式是什麼?正確方向的任何指針都是不勝感激。我有一個表單提交到的url的處理程序。該處理程序的代碼是:解析傳入的http post請求java android

import java.io.IOException; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpException; 
import org.apache.http.HttpRequest; 
import org.apache.http.HttpResponse; 
import org.apache.http.entity.ContentProducer; 
import org.apache.http.entity.EntityTemplate; 
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; 
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 = null; 
      OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); 
      if(validatePassword()==true){ 
      resp ="<html><head></head><body><h1>Home<h1><p>Success.</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 validatePassword(){ 
boolean pass=false; 
//parse request body here and check for the password if true return true/else false 
return pass; 
} 


} 

回答

5

環顧四周後,我找到了解決方案。在處理方法中添加以下內容可以實現這一點。感謝原始海報 。 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(); 
} 
} 
2

我很抱歉如果這不是你所問的,所以如果不是,請告訴我。

您可以使用JSONObject返回是否驗證該密碼是否正確。

例如,如果密碼正確,你可以存儲HTTP結果爲:

{"status":200,"confirmed":"true"} 

或「假」。

當您從HTTP發佈請求中返回時,可以將此結果存儲爲字符串,然後在其中創建一個JSONObject。例如:

// Send the URL to a postRequest function and return the result as a String 
String output = makePostRequest(url); 

// Parse the String as a JSONObject and receive whether or not the login was confirmed 
JSONObject o = new JSONObject(output); 
String confirmed = o.getString("confirmed"); 
if (confirmed.equals("true")) { 
    // Password confirmed - redirect user to success page 
} else { 
    // Password incorrect - redirect user to failure page 
} 

注:的情況下,你需要的是如何從POST請求接收響應代碼的想法,這裏的一些示例代碼:

String output = {}; 

// Use bufferedreader and stringbuilder to build an output string (where conn is your HTTPUrlConnection object you used to make the post request  
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
StringBuilder sb = new StringBuilder(); 
String line; 

// Loop through response to build JSON String 
while((line = br.readLine()) != null) { 
    sb.append(line + "\n"); 
} 

// Set output from response 
output = sb.toString(); 

現在output是字符串你可以變成一個JSONObject。

這有幫助嗎?


編輯:
好了,你會得到將在{"password":"somepassword"}格式字符串。解析這個,試試這個:

String s = /* the string in the format {"password":"somepassword"} */ 
JSONObject o = new JSONObject(s); 
String password = o.getString("password"); 
if (password.equals(random_password_at_beginning_of_webservice) { 
    // Password confirmed - redirect user to success page 
} else { 
    // Password incorrect - redirect user to failure page 
} 
+0

謝謝你把back.Well它是一個沒有database.When android的服務啓動它顯示一個password.I想驗證它是否是請求服務的同一用戶的單個用戶的應用程序。我之前沒有使用JSON,但看起來像是一個可行的選項,只要在android上有一個JSON解析器。 – kalz 2012-08-08 12:15:04

+0

'getString'返回String,而不是'boolean' – Serjio 2012-08-08 12:15:52

+0

我的歉意,那是固定的:)。 – Mxyk 2012-08-08 12:16:32