【发布时间】:2014-09-17 11:28:34
【问题描述】:
我在获取一条线路时遇到了一些问题。我需要获取服务器响应的一行,如果是则注册失败,如果不是则一切正常。这是一段代码,我在请求后从服务器获取响应:
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://guiddy.fosslabs.ru/users/sign_in");
post.setEntity(new UrlEncodedFormEntity(pairs));
response = client.execute(post);
String responseStr = EntityUtils.toString(response.getEntity());
Log.d("POST", "Response " + responseStr);
我的回应是:
09-17 13:47:49.556: D/POST(3220): <html>
09-17 13:47:49.556: D/POST(3220): <head>
09-17 13:47:49.556: D/POST(3220): <title>KFU Guide</title>
09-17 13:47:49.556: D/POST(3220): <link href="/assets/application-322e6975fe46a0c54a3827debe6d5726.css" media="all" rel="stylesheet" type="text/css" />
09-17 13:47:49.556: D/POST(3220): <script src="/assets/application-803c643ca444e7c5678978757763fb7e.js" type="text/javascript"></script>
09-17 13:47:49.556: D/POST(3220): <meta content="authenticity_token" name="csrf-param" />
09-17 13:47:49.556: D/POST(3220): <meta content="uN8Hb2qA2nBr1euV6xKh4l8PGQYyzYviH5Ba5Ncwl24=" name="csrf-token" />
09-17 13:47:49.556: D/POST(3220): <!--[if lt IE 9]>
09-17 13:47:49.556: D/POST(3220): <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
09-17 13:47:49.556: D/POST(3220): <![endif]-->
09-17 13:47:49.556: D/POST(3220): </head>
09-17 13:47:49.556: D/POST(3220): <body>
09-17 13:47:49.556: D/POST(3220): <header class="navbar navbar-fixed-top navbar-inverse">
09-17 13:47:49.556: D/POST(3220): <div class="navbar-inner">
09-17 13:47:49.556: D/POST(3220): <div class="row">
09-17 13:47:49.556: D/POST(3220): <a href="/home" id="logo">KFU Guide</a>
09-17 13:47:49.556: D/POST(3220): <nav>
09-17 13:47:49.556: D/POST(3220): <ul class="nav pull-right">
09-17 13:47:49.556: D/POST(3220): <div class="exit">
09-17 13:47:49.556: D/POST(3220): <a href="/users/sign_in">Вход</a> |
09-17 13:47:49.556: D/POST(3220): <a href="/users/sign_up">Зарегистрироваться</a>
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220): </ul>
09-17 13:47:49.556: D/POST(3220): </nav>
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220):
09-17 13:47:49.556: D/POST(3220): <div class="space">
09-17 13:47:49.556: D/POST(3220): <nav>
09-17 13:47:49.556: D/POST(3220): <ul class="nav pull-left">
09-17 13:47:49.556: D/POST(3220): <li><a href="/rooms">Аудитории</a></li>
09-17 13:47:49.556: D/POST(3220): <li><a href="/events">События</a></li>
09-17 13:47:49.556: D/POST(3220): <li><a href="/find_empty_room">Поиск свободной аудитории</a></li>
09-17 13:47:49.556: D/POST(3220): </ul>
09-17 13:47:49.556: D/POST(3220): </nav>
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220):
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220): </header>
09-17 13:47:49.556: D/POST(3220): <br>
09-17 13:47:49.556: D/POST(3220): <br>
09-17 13:47:49.556: D/POST(3220): <div class="container">
09-17 13:47:49.556: D/POST(3220): <div class="alert alert-alert"> Invalid email or password. </div>
09-17 13:47:49.556: D/POST(3220): <h1>Вход в систему</h1>
09-17 13:47:49.556: D/POST(3220): <div class="row">
09-17 13:47:49.556: D/POST(3220): <div class="span6 offset3">
09-17 13:47:49.556: D/POST(3220): <form accept-charset="UTF-8" action="/sessions/user" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="uN8Hb2qA2nBr1euV6xKh4l8PGQYyzYviH5Ba5Ncwl24=" /></div>
09-17 13:47:49.556: D/POST(3220): <label for="user_email">Email</label>
09-17 13:47:49.556: D/POST(3220): <input autofocus="autofocus" id="user_email" name="user[email]" size="30" type="email" value="deniknovik@gmail.com" />
09-17 13:47:49.556: D/POST(3220): <label for="user_password">Password</label>
09-17 13:47:49.556: D/POST(3220): <input id="user_password" name="user[password]" size="30" type="password" />
09-17 13:47:49.556: D/POST(3220): <input name="user[remember_me]" type="hidden" value="0" /><input id="user_remember_me" name="user[remember_me]" type="checkbox" value="1" />
09-17 13:47:49.556: D/POST(3220): <label for="user_remember_me">Remember me</label>
09-17 13:47:49.556: D/POST(3220): <div class="center">
09-17 13:47:49.556: D/POST(3220): <input class="btn btn-large btn-primary" name="commit" type="submit" value="Войти" />
09-17 13:47:49.556: D/POST(3220): <br>
09-17 13:47:49.556: D/POST(3220): <br>
09-17 13:47:49.556: D/POST(3220): <a href="/users/auth/facebook" class="services"><img alt="Facebook" height="32" src="/assets/fb-438d0240ee7e9ca267a8b8b0c984f531.png" width="32" /></a>
09-17 13:47:49.556: D/POST(3220): <a href="/users/auth/twitter" class="services"><img alt="Facebook" height="34" src="/assets/tw-f3aa14c332eace82e49dc3776b3ec8cc.png" width="34" /></a>
09-17 13:47:49.556: D/POST(3220): <a href="/users/auth/vkontakte" class="services"><img alt="Facebook" height="32" src="/assets/vk-d772cd8b00aa8f4a0ec821ca6aa649bc.png" width="32" /></a>
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220):
09-17 13:47:49.556: D/POST(3220): <a href="/users/sign_up">Sign up</a><br />
09-17 13:47:49.556: D/POST(3220): <a href="/users/password/new">Forgot your password?</a><br />
09-17 13:47:49.556: D/POST(3220): </form> </div>
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220):
09-17 13:47:49.556: D/POST(3220): </div>
09-17 13:47:49.556: D/POST(3220): </body>
09-17 13:47:49.556: D/POST(3220): </html>
我只需要得到这一行:
<div class="alert alert-alert"> Invalid email or password. </div>
【问题讨论】:
标签: java android html request response