Android Webservice会话管理

时间:2012-07-20 14:22:16

标签: android service web

我正在使用一个使用webservices的android应用程序。首先,应用程序通过下面编写的httppost方法登录。

public void postData() throws ParseException, IOException 
{
    httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.0.46:8080/login");
    httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");;
    HttpResponse responses = null;

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("LoginForm[email]", "t@a.com"));
    nameValuePairs.add(new BasicNameValuePair("LoginForm[password]", "tttttt"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    responses = httpclient.execute(httppost);

    HttpEntity responseEntity = responses.getEntity();
    String apitoken = EntityUtils.toString(responseEntity);

} 

现在,apitoken是关键(视频键),它将返回服务有关该用户的其他数据

public void getCategories {
      HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost("http://192.168.0.46:8080/usercategories");
          httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");;


          HttpResponse responses = null;
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
          nameValuePairs.add(new BasicNameValuePair("api_token", apitoken));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         try {
              responses = httpclient.execute(httppost);
         } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
          } catch (IOException e) {
              // TODO Auto-generated catch block
          }


         HttpEntity responseEntity = responses.getEntity();
        String category =EntityUtils.toString(responseEntity) ;
}
}

现在问题是服务器会话密钥(api_token)没有得到识别.. 我在服务器上检查了与应用程序从服务器获取的密钥完全相同的密钥。 我在iPhone中实现了相同的web服务。哪个工作正常.. 我是android新手,我不明白是什么问题。我是否还应该在客户端或其他方面保持会话。 欢迎任何帮助

0 个答案:

没有答案