1

我正在开发一个使用 web 服务的 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 是密钥(seesion 密钥),它将返回服务关于该用户的其他数据

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 新手,我不明白是什么问题。我是否还应该在客户端或其他地方保持会话。欢迎任何帮助

4

0 回答 0