解析传入的http post请求java android

解析传入的http post请求java android,android,parsing,http-request,Android,Parsing,Http Request,我在安卓网络服务器上工作。当我在emulator浏览器上转到localhost:8080时,它提供一个带有密码字段的页面/表单。在成功验证密码后,我想将用户重定向到成功/失败页面。读取传入http post请求并解析密码字段以进行验证的最佳方式是什么?任何指向正确方向的指针都将不胜感激。我有一个表单提交到的url的处理程序。处理程序的代码是: import java.io.IOException; import java.io.OutputStream; import java.io.Outpu

我在安卓网络服务器上工作。当我在emulator浏览器上转到localhost:8080时,它提供一个带有密码字段的页面/表单。在成功验证密码后,我想将用户重定向到成功/失败页面。读取传入http post请求并解析密码字段以进行验证的最佳方式是什么?任何指向正确方向的指针都将不胜感激。我有一个表单提交到的url的处理程序。处理程序的代码是:

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


 }
import java.io.IOException;
导入java.io.OutputStream;
导入java.io.OutputStreamWriter;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpException;
导入org.apache.http.HttpRequest;
导入org.apache.http.HttpResponse;
导入org.apache.http.entity.ContentProducer;
导入org.apache.http.entity.EntityTemplate;
导入org.apache.http.protocol.HttpContext;
导入org.apache.http.protocol.HttpRequestHandler;
导入android.content.Context;
公共类LoginHandler实现HttpRequestHandler{
私有上下文=null;
公共登录句柄(上下文){
this.context=上下文;
}
@凌驾
公共无效句柄(最终HttpRequest请求、HttpResponse响应、,
HttpContext(HttpContext)引发HttpException,IOException{
HttpEntity实体=新的EntityTemplate(新的ContentProducer(){
public void writeTo(最终输出流超出流)引发IOException{
字符串resp=null;
OutputStreamWriter writer=新的OutputStreamWriter(扩展流,“UTF-8”);
if(validatePassword()==true){
resp=“Home成功。

”; } 否则{resp=“Home登录失败。

“;} 作家、作家(resp); writer.flush(); } }); setHeader(“内容类型”、“文本/html”); 响应。设置实体(实体); } 布尔验证密码(){ 布尔传递=假; //在此处解析请求正文,如果为true,则检查密码返回true/else false 回程通行证; } }
如果这不是您要问的问题,我很抱歉,如果不是,请告诉我

您可以使用返回密码是否被验证为正确

例如,如果密码正确,则可以将HTTP结果存储为:

{"status":200,"confirmed":"true"} 
否则“假”

当您从HTTP Post请求返回时,可以将此结果存储为字符串,然后从中生成一个
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
}

在环顾四周很久之后,我找到了解决办法。在handle方法中添加以下内容可以实现此目的。多亏了原始海报


感谢您回来。这是一个没有数据库的单用户应用程序。当android服务启动时,它会显示一个密码。我想验证它是否是请求该服务的同一用户。我以前没有使用过JSON,但如果android上有JSON解析器,它看起来是一个可行的选项。
getString
返回字符串,而不是
boolean
我的道歉,这是固定的:)@Mike Gates我花了一些时间来研究JSON。发布的数据JSON结构应该类似于{“password”:“somepassword”}。服务器是android手机,表单发布到android手机上的localhost。因此,我想以json格式解析上述字符串,并根据android手机上启动WebServerService时生成的随机密码检查密码字段。希望我说清楚。如果您想了解更多信息,请让我知道。谢谢。我给我的答案贴了一个编辑。看看这是否对你有所帮助。如果不是的话,我可能需要更多的解释,说明你到底想帮什么忙。
        if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
Log.v("RequestBody", EntityUtils.toString(entity, "UTF-8"));
entity.consumeContent();
}
}