【发布时间】:2014-05-21 02:22:52
【问题描述】:
我是 Android 开发的新手,但我正在尝试为 Opencart 做一个应用程序,以允许用户进入他们自己的商店来管理它。
让我们进入正题。为了从商店获取信息,我创建了一个页面,其中所有信息都以 XML 形式呈现,因此想法是用户登录,然后重定向到该页面并使用 http 响应,解析 xml 和瞧!
我已经有了 xml 解析器,但我在使用 http 连接时遇到了一些困难。让我再解释一下:
基本上,要登录任何商店,您需要访问 www.example.com/admin(我将使用我的测试在线地址来查看是否有人能够帮助我),在这种情况下为 http://www.onlineshop.davisanchezplaza.com/admin 。一旦我们到达页面,我们就会到达登录系统。登录系统使用 post 发送用户名:admin 和密码:admin 并重定向到 http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/login,一旦验证您的身份,它就会给您一个 Token(这里我开始遇到一些问题)。 http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/home&token=8e64583e003a4eedf54aa07cb3e48150 。好吧,到这里为止,我很好,实际上开发了一个可以做到这里的应用程序,实际上我可以“硬编码”从它发送给我的 http 响应中读取令牌(实际上不是很好)。 这是我的第一个问题:如何从 HTTPresponse 中获取令牌值?(现在,正如我所说,我只能通过读取所有响应来获取令牌,如果我们找到字符串令牌=,接下来会发生什么……不好)。
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost("http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/login");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "admin"));
nameValuePairs.add(new BasicNameValuePair("password", "admin"));
try{
Log.d(DEBUG_TAG, "Try ");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096);
Log.d(DEBUG_TAG, "br :" + br);
String line;
while ((line = br.readLine()) != null) {
Log.d(DEBUG_TAG, "br :" + line);
if(line.contains("token=")){
int index = line.indexOf("token=");
String aux = line.substring(index + "token=".length(), index + 32 + "token=".length());
token = aux; //Yes, I know, its not the best way.
break;
}
}
} catch (IOException e) {
Log.d(DEBUG_TAG, "Finally");
}
第二个问题,(以及更多重要),现在有了令牌(在示例中为 8e64583e003a4eedf54aa07cb3e48150),我需要转到route android/home 生成的xml信息在哪里。 (http://onlineshop.davidsanchezplaza.com/admin/index.php?route=android/home2&token=8e64583e003a4eedf54aa07cb3e48150)。正如我正在阅读的那样,在 httpget 中,我们可以设置参数,或者直接发送带有参数的 url。是在这一步停止的地方。也许是中国的互联网连接,也许(最肯定)我做错了什么。有时它只是来了超时连接,其他的只是让我回到登录页面。
这是我的代码(编辑:我是一个菜鸟,并没有创建 httpclient 来接收答案,对不起!):
String s = "http://onlineshop.davidsanchezplaza.com/admin/index.php?route=common/home&token=";
String tot = s.concat(token);
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpGet httpget = new HttpGet(tot);
try{
Log.d(DEBUG_TAG, "Try ");
HttpResponse response = httpClient.execute(httpget);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096);
Log.d(DEBUG_TAG, "br :" + br);
} catch (IOException e) {
Log.d(DEBUG_TAG, "Finally");
}
我不需要有人告诉我怎么做,只需要一点指导来解决问题,我非常感谢您提供的任何评论或帮助,或额外的文档:)。
作为奖励,如果有人能给我更多关于如何测试 http get 的详细信息,我将不胜感激,我只知道如何在网络浏览器中编写它,并且工作正常。
【问题讨论】: