虽然我不熟悉Android开发,但我试图为Opencart做一个应用程序,允许用户在自己的商店中输入以管理它。Http获取传递参数
让我们来谈谈。为了从商店获得信息,我创建了一个页面,其中所有信息都以XML格式显示,因此我们的想法是用户登录,然后重定向到此页面并使用http响应,解析xml和voilá!.
我已经有了XML解析器,但是Im在http连接上遇到了一些困难。让我再解释一下:
基本上,要登录任何商店,您需要访问www.example.com/admin(我将使用我的测试在线地址,以查看是否有人能够帮助我),在这种情况下为http://www.onlineshop.davisanchezplaza.com/admin。一旦我们到达页面,我们到达登录系统。登录系统使用post来发送用户名:admin和password: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响应中读取令牌(实际上不是很好)。 这里是我的第一个问题:如何从HTTP响应令牌值?(到目前为止,正如我所说的,我只能通过读取所有响应来获取标记,并且如果我们找到字符串标记=,请接下来的内容......不太好)。
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),我需要去路线的Android /家中,生成的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的更多详细信息,我将不胜感激,我只知道如何在网页浏览器中编写它,并且工作正常。
嗨,谢谢你的回应。我确实得到了第一个反应,虽然我用boucle做了,但我能找到这个令牌。为了您的观点,我不需要,因为捕获通常是例外情况,例如, IOException异常。 –