gpt4 book ai didi

android - http获取传参

转载 作者:可可西里 更新时间:2023-11-01 17:02:20 25 4
gpt4 key购买 nike

我是 Android 开发的新手,但我正在尝试为 Opencart 开发一个应用程序,以允许用户进入他们自己的商店进行管理。

让我们进入正题。为了从商店获取信息,我创建了一个页面,其中所有信息都以 XML 格式显示,所以我的想法是用户登录,然后重定向到该页面并使用 http 响应解析 xml 和 voilá!。

我已经有了 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 响应中读取 token (实际上不是很好)。我的第一个问题来了:如何从 HTTPresponse 获取 token 值?(到目前为止,正如我所说,我只能通过读取所有响应来获取 token ,如果我们找到字符串 token =,拿接下来的东西......不好)。

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

第二个问题(以及更多重要),现在有了 token (在示例中为 8e64583e003a4eedf54aa07cb3e48150),我需要转到route android/home 生成xml信息的地方。 (http://onlineshop.davidsanchezplaza.com/admin/index.php?route=android/home2&token=8e64583e003a4eedf54aa07cb3e48150)。正如我所阅读的,在 httpget 中,我们可以设置参数,也可以直接发送 url,其中参数已经包含在 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 的详细信息,我将不胜感激,我只知道如何在网络浏览器中编写它,并且工作正常。

最佳答案

我上次为 Android 做点什么已经有一段时间了,但这里是我的建议:

  1. 为了从 Android 应用程序登录到 OpenCart 管理,我建议创建一个新的移动登录页面,例如而不是访问 http://yourstore.com/admin/ 将您重定向到 http://.../admin/index.php?route=common/login创建您自己的操作,例如androidLogin() 在此 Controller 中(admin/controller/common/login.php 并且您将通过 http://yourstore.com/admin/直接访问它index.php?route=common/login/androidLogin. 为什么要执行特殊操作?因为默认登录操作将用户(使用普通浏览器)重定向到主页,同时将安全 token 设置到查询字符串部分的 URL 中。在您自己的操作中,您不会重定向,而是使用包含此安全 token 的 XML 进行响应,以便您可以使用您的 XML 解析器轻松提取该 token 。

  2. 我无法准确解决第二个问题,但据我所知,我以不同的方式传递查询字符串(现在我在互联网上找不到任何类似的解决方案)。

关于android - http获取传参,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22936189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com