1

私はiOSに別のアプリケーションを構築しており、ユーザーがアプリにログインすると、サーバーはそのセッションにログインします。iOS アプリでは、通常のセッションとアプリからのすべてのリクエストでユーザーがまだログインしていることがわかる限り、セッションは維持されます。

これは Android アプリでも同じだと思いましたが、ログインした後、次のアクティビティに切り替えてから、サーバーにリクエストを送信してページに入力します。私が得ている結果は、ユーザーがログインしていないということです。

サーバー上でセッションを維持していないことを理解していないという違いはありますか?

すべてのアクティビティを拡張する基本クラスがあり、基本クラス内にこの保護されたクラスがあります。私はそれを使用してすべてのリクエストを送信します。

protected class API extends AsyncTask <Void, Void, String>
{
    public String RequestName;
    public String RequestType;
    public String RequestUrl;
    public List<NameValuePair> RequestParameters;

    public API(String Name, String Type, String Url, List<NameValuePair> params)
    {
        RequestName = Name;
        RequestType = Type;
        RequestUrl = Url;
        RequestParameters = params;
    }

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException 
    {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n>0) 
        {
            byte[] b = new byte[4096];
            n =  in.read(b);
            if (n>0) 
                out.append(new String(b, 0, n));
        }
        return out.toString();
    }

    @Override
    protected String doInBackground(Void... params) 
    {


        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();

        String text = null;

        if(RequestType == "post")
        {
            HttpPost p = new HttpPost("http://www.ehrx.com/api/" + RequestUrl);

            try 
            {
                p.setEntity(new UrlEncodedFormEntity(RequestParameters));
                HttpResponse response = httpClient.execute(p, localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } 
            catch (Exception e) 
            {
                return e.getLocalizedMessage();
            }
        }
        else
        {
            HttpGet httpGet = new HttpGet("http://www.ehrx.com/api/" + RequestUrl);

            try 
            {
                HttpResponse response = httpClient.execute(httpGet, localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } 
            catch (Exception e) 
            {
                return e.getLocalizedMessage();
            }
        }



        return text;
    }

    protected void onPostExecute(String results) 
    {
        HandleResult(results, RequestName);

    }
}
4

2 に答える 2

0

これは、シングルトン クラスを使用して簡単に修正できます (すべての http 要求で同じ HTTP クライアントを使用する必要があります)。

于 2014-05-26T23:48:02.530 に答える