Android 6设备上的Tidhttp fatalexception

时间:2016-07-19 16:42:18

标签: android google-maps http https c++builder

我有一个使用tidhttp例程从谷歌加载静态地图的功能。这在android 5中效果很好,但是在android 6中它会抛出一个fatalexception。我知道在android 6上有一些tidhttp和https://的罪魁祸首(这里有一些关于此的帖子),但我只使用http://并且遇到问题。由于我手头没有任何Android 6设备,我无法对此进行全面测试。

是否有其他人在谷歌静态地图(http)上使用Android 6设备上的tidhttp时出现问题?你是怎么解决的?

--- 2015.07.21:

更改:

在我的问题所在的地方添加代码:我看不到TIdHTTPThread表单中的任何错误......并且它(如上所述)函数在andriod5中...

TIdHTTPThread.h

//--------------------------------------------------------------------------- 

#ifndef IdHTTPThreadH
#define IdHTTPThreadH

#include <system.classes.hpp>
//---------------------------------------------------------------------------

class TDownloadThread : public TThread
{
protected:
    virtual void __fastcall Execute();

private:
    String searchString;

public:
     TMemoryStream *Strm;

    __fastcall TDownloadThread();

    __fastcall int setSearchString(String new_searchString);

};
#endif

IdHTTPThread.cpp

//---------------------------------------------------------------------------

#pragma hdrstop

#include "IdHTTPThread.h"
#include <idhttp.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)

__fastcall TDownloadThread::TDownloadThread():TThread(true)
{
    FreeOnTerminate = true;
    Strm = new TMemoryStream;
    searchString ="";
}

//---------------------------------------------------------------------------

void __fastcall TDownloadThread::Execute()
{
    TIdHTTP *Http = new TIdHTTP(NULL);

    Http->Request->UserAgent="Mozilla/3.0 (compatible; FuelPumpFree)";

    Http->Get(searchString, Strm);

    Strm->Position=0;

}

//---------------------------------------------------------------------------

__fastcall int TDownloadThread::setSearchString(String new_searchString)
{
    searchString = new_searchString;

    return 0;
}

以下是使用上述代码从google map api中提取静态地图的功能。

下面的代码是ViewMap.cpp

的头文件中需要的定义

ViewMap.h:

//---------------------------------------------------------------------------

#ifndef ViewMapH
#define ViewMapH
//---------------------------------------------------------------------------
#include <System.Sensors.Components.hpp>
#include <FMX.WebBrowser.hpp>
#include <FMX.TabControl.hpp>
#include <Firedac.Comp.Client.hpp>
#include <fmx.stdctrls.hpp> 

#include "IdHTTPThread.h"

//---------------------------------------------------------------------------

struct str_mapdata
{
   float latitude;
   float longitude;
   int scale;
   String key;

};

//---------------------------------------------------------------------------

class TViewMapForm: public TRectangle
{
protected:

    TRectangle *badgerect, *lb_lat_rect, *lb_lon_rect, *mainrect, *hdrrect,
            *ftrrect;

    TButton *btn_pos_refresh, *btn_close;

    TImage *mapimage;   //Substitutes TWebbrowser which not aligns correctly.

    TShadowEffect *badgeshadow;
    TGlowEffect *badgeglow;

    TToolBar *tb_ftr;

    TFDMemTable *FDMemTable_localdata;

    TLabel *lb_longitude, *lb_latitude;

    TLocationSensor *locationsensor;

    //TWebBrowser *webbrowser;

    TTrackBar *tb_mapfocus;
    TSwitch *sw_focus;

    str_mapdata mapdata;

private:
    void __fastcall DownloadMap(float latitude, float longitude);
    void __fastcall DownloadMap(String mapstring);

    void __fastcall DownloadMapFinished(TObject *Sender);


public:

    __fastcall int updateMapData(float new_latitude, float new_longitude,
        String new_key);

    __fastcall int updateMap();

    __fastcall TViewMapForm(TComponent *Owner,
        TFDMemTable *FDMemTable_localdata);

    __fastcall void locationsensorchanged(TObject *Sender,
        const TLocationCoord2D &OldLocation,
        const TLocationCoord2D &NewLocation);

    __fastcall void mapfocustrackchange(TObject *Sender);

    __fastcall void btncloseclick(TObject *Sender);

    __fastcall void btnrefreshclick(TObject *Sender);

};



#endif

以下代码是从我的项目中提取的......

评论//&lt;&lt;标记信息

//---------------------------------------------------------------------------

#pragma hdrstop

#include "FuelPumpUnit1.h"  //<<This is not included in this post
#include "ViewMap.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

//---------------------------------------------------------------------------

__fastcall TViewMapForm::TViewMapForm(TComponent *Owner,
    TFDMemTable *new_FDMemTable_localdata):TRectangle(Owner)
{
    //Creating objects
    badgerect = new TRectangle(Owner);
    lb_latitude = new TLabel(Owner);
    lb_longitude = new TLabel(Owner);
    lb_lat_rect = new TRectangle(Owner);
    lb_lon_rect = new TRectangle(Owner);
    btn_pos_refresh = new TButton(Owner);
    btn_close = new TButton(Owner);
    hdrrect = new TRectangle(Owner);
    mainrect = new TRectangle(Owner);
    tb_ftr = new TToolBar(Owner);
    ftrrect = new TRectangle(Owner);

    //webbrowser = new TWebBrowser(Owner); << not used due to alignemt error
    mapimage = new TImage(Owner);

    locationsensor = new TLocationSensor(Owner);

    tb_mapfocus = new TTrackBar(Owner);

    //Copying information

    FDMemTable_localdata = new_FDMemTable_localdata;

    sw_focus=new TSwitch(Owner);

    sw_focus->Width = 50;
    sw_focus->IsChecked = false;
    sw_focus->Align = TAlignLayout::Left;

    //setting mapdata
    mapdata.latitude=0;
    mapdata.longitude=0;
    mapdata.scale=1;

    //Main headderrect
    hdrrect->Height = 50;
    hdrrect->Align = TAlignLayout::Top;
    hdrrect->Fill->Color=TAlphaColor(claBlack);

    //Main footerrect
    ftrrect->Height = 50;
    ftrrect->Align = TAlignLayout::Bottom;
    ftrrect->Fill->Color=TAlphaColor(claBlack);

    tb_ftr->Height = 50;
    tb_ftr->Align = TAlignLayout::Bottom;
    tb_ftr->TintColor=TAlphaColor(claBlack);
 // tb_ftr->Fill->Color=TAlphaColor(claBlack);

    tb_mapfocus->Align = TAlignLayout::Bottom;
    tb_mapfocus->Value = 5;
    tb_mapfocus->Min=5;
    tb_mapfocus->Max=15;
    tb_mapfocus->Frequency=1;
    tb_mapfocus->OnChange = &mapfocustrackchange;

    //Labelrect Latitude
    lb_lat_rect->Width=110;
    lb_lat_rect->Align=TAlignLayout::Right;
    lb_lat_rect->Fill->Color=TAlphaColor(claBlack);

    //Labelrect Longitude
    lb_lon_rect->Width=110;
    lb_lon_rect->Align=TAlignLayout::Right;
    lb_lon_rect->Fill->Color=TAlphaColor(claBlack);

    //Label latitude
    lb_latitude->StyledSettings = lb_latitude->StyledSettings >>
        TStyledSetting::Size >> TStyledSetting::Family >>
        TStyledSetting::Style >> TStyledSetting::FontColor >>
        TStyledSetting::Other;
    lb_latitude->Text="Latitude:\n000.000000";
    lb_latitude->Align=TAlignLayout::Client;
    lb_latitude->TextSettings->Font->Size=18;
    lb_latitude->TextSettings->FontColor=TAlphaColor(claYellow);

    //Label longitude
    lb_longitude->StyledSettings = lb_longitude->StyledSettings >>
        TStyledSetting::Size >> TStyledSetting::Family >>
        TStyledSetting::Style >> TStyledSetting::FontColor >>
        TStyledSetting::Other;
    lb_longitude->Text="Longitude:\n000.000000";
    lb_longitude->Align=TAlignLayout::Client;
    lb_longitude->TextSettings->Font->Size=18;
    lb_longitude->TextSettings->FontColor=TAlphaColor(claYellow);



    //Btn refresh
    btn_pos_refresh->Text = "Pos";
    btn_pos_refresh->Align = TAlignLayout::Left;
    btn_pos_refresh->StyleLookup="refreshtoolbutton";
    btn_pos_refresh->IconTintColor = TAlphaColor(claRed);
    btn_pos_refresh->Width = 48;
    btn_pos_refresh->OnClick = &btnrefreshclick;

    //Btn close
    btn_close->Text = "Cls";
    btn_close->Align = TAlignLayout::MostRight;
    btn_close->StyleLookup="replytoolbutton";
    btn_close->IconTintColor = TAlphaColor(claRed);
    btn_close->Images = FuelPumpForm->ImageList_buttons;
    btn_close->ImageIndex = 10;
    btn_close->Width = 55;
    btn_close->OnClick = &btncloseclick;

  //btn_pos_refresh->OnClick = &btn_pos_refresh_click;

    mainrect->Align=TAlignLayout::Client;
    mainrect->Fill->Color=TAlphaColor(claBlack);

    //webbrowser->Align = TAlignLayout::Client;
    mapimage->Align=TAlignLayout::Client;


    //Badgerect
    badgerect->Fill->Color=TAlphaColor(claBlack);
    badgerect->Stroke->Color=TAlphaColor(claNull);
    badgerect->Height=100;
    badgerect->Align=TAlignLayout::Client;

    //Padding
    badgerect->Padding->Top=5;
    badgerect->Padding->Bottom=5;
    badgerect->Padding->Left=5;
    badgerect->Padding->Right=5;

    //Margins
    badgerect->Margins->Top=15;
    badgerect->Margins->Left=15;
    badgerect->Margins->Right=15;
    badgerect->Margins->Bottom=5;

    badgeshadow = new TShadowEffect(Owner);
    badgeglow = new TGlowEffect(Owner);
    badgeglow->GlowColor = TAlphaColor(claWhite);


    this->Fill->Color=TAlphaColor(claNull);
    this->Stroke->Color=TAlphaColor(claNull);
    this->Stroke->Thickness=0;
    this->Height = 390;
    this->Position->Y=-100;
    this->Align=TAlignLayout::Top;

    badgerect->AddObject(badgeshadow);

    lb_lat_rect->AddObject(lb_latitude);
    lb_lon_rect->AddObject(lb_longitude);

    /*webbrowser->Navigate("https://maps.googleapis.com/maps/api/staticmap"
    "?size=320x260&scale=1&zoom=15&maptype=roadmap&"
    "key=AIzaSyCjc63q60cIHgcpj0R88UsDBpmVEC9sVK4");
    */
//<< Line above disabled due to alingment errors with TWebBrowser component

    //mainrect->AddObject(webbrowser);
    mainrect->AddObject(mapimage);


    hdrrect->AddObject(btn_close);
    hdrrect->AddObject(btn_pos_refresh);
    hdrrect->AddObject(lb_lat_rect);
    hdrrect->AddObject(lb_lon_rect);

    //ftrrect->AddObject(tb_ftr);

    //ftrrect->AddObject(sw_focus);
    ftrrect->AddObject(tb_mapfocus);

    badgerect->AddObject(hdrrect);
    badgerect->AddObject(mainrect);
    badgerect->AddObject(ftrrect);

    this->AddObject(badgeglow);
    this->AddObject(badgerect);
    //this->AddObject(mainrect);



    locationsensor->OnLocationChanged=&locationsensorchanged;

    locationsensor->Active = true;

    //updateMap();
}

//---------------------------------------------------------------------------

int __fastcall TViewMapForm::updateMapData(float new_latitude,
    float new_longitude, String new_key)
{
    mapdata.longitude = new_longitude;
    mapdata.latitude = new_latitude;
    mapdata.key = new_key;  //<<API key from Google

    AnsiString as_lattext, as_lontext;

    as_lattext.sprintf("Latitude:\n%010.6f",
    mapdata.latitude);

    as_lontext.sprintf("Longitude:\n%010.6f",
        mapdata.longitude);

    lb_latitude->Text=as_lattext;

    lb_longitude->Text=as_lontext;

}

//---------------------------------------------------------------------------

void __fastcall TViewMapForm::DownloadMap(float latitude, float longitude)
{
    TDownloadThread *Thread = new TDownloadThread();
    Thread->OnTerminate = &DownloadMapFinished;

    Thread->setSearchString("https://maps.googleapis.com/maps/api/staticmap"
        "?center="+FloatToStr(latitude)+
        ","+FloatToStr(longitude)+
        "&zoom=16&size=320x260&maptype=roadmap&markers=color:red"
        "|label:?|"+FloatToStr(latitude)+","+
        FloatToStr(longitude)+"&key="+mapdata.key);

    Thread->Start();

}
//---------------------------------------------------------------------------

void __fastcall TViewMapForm::DownloadMap(String mapstring)
{
    TDownloadThread *Thread = new TDownloadThread();
    Thread->OnTerminate = &DownloadMapFinished;
    Thread->setSearchString(mapstring);
    Thread->Start();

}

//---------------------------------------------------------------------------

void __fastcall TViewMapForm::DownloadMapFinished(TObject *Sender)
{
    TDownloadThread *Thread = static_cast<TDownloadThread *>(Sender);

    if(!Thread->FatalException)
        mapimage->Bitmap->LoadFromStream(Thread->Strm);  

//<< Line above fails with no good reason on Androd 6  
//Android 5 & 4 in the other hand... is OK!!  Why ? 

    else ShowMessage("Error loading map from Google!");
}


//---------------------------------------------------------------------------
__fastcall void TViewMapForm::locationsensorchanged(TObject *Sender,
    const TLocationCoord2D &OldLocation,
    const TLocationCoord2D &NewLocation)
{
    char LDecSeparator = FormatSettings.DecimalSeparator;
    FormatSettings.DecimalSeparator = '.';

    mapdata.longitude=NewLocation.Longitude;
    mapdata.latitude =NewLocation.Latitude;

    AnsiString as_lattext, as_lontext;

    as_lattext.sprintf("Latitude:\n%010.6f",
        NewLocation.Latitude);

    as_lontext.sprintf("Longitude:\n%010.6f",
        NewLocation.Longitude);

    lb_latitude->Text=as_lattext;

    lb_longitude->Text=as_lontext;

    locationsensor->Active=false;

    btn_pos_refresh->IconTintColor=TAlphaColor(claYellow);

    FormatSettings.DecimalSeparator = LDecSeparator;

    updateMap();

}

//-------------------------------------------------------------------------- -

__fastcall int TViewMapForm::updateMap()
{
    char LDecSeparator = FormatSettings.DecimalSeparator;
    FormatSettings.DecimalSeparator = '.';

    String mapstring,midstring, lon_string, lat_string;

    mapstring = "https://maps.googleapis.com/maps/api/staticmap";

    //Adding size
    mapstring += "?size=320x260";

    //Adding scale
    mapstring +="&scale=1";

    //Adding zoom

    if(tb_mapfocus->Value>5)
    {
        mapstring += "&zoom="+IntToStr((int)tb_mapfocus->Value);

        if((FuelPumpForm->globaldata.region=="Region")
            &&(FuelPumpForm->globaldata.road=="Road"))
        {
            mapstring += "&center="+FloatToStr(mapdata.latitude)+","+
            FloatToStr(mapdata.longitude);
        }
    }

    //Adding type
    mapstring +="&maptype=roadmap";

    //You marker

    if((FuelPumpForm->globaldata.region=="Region")
        &&(FuelPumpForm->globaldata.road=="Road"))
    {
        mapstring += "&markers=color:black|label:X|"+
        FloatToStr(mapdata.latitude)+","+FloatToStr(mapdata.longitude);
    }


    //if(FDMemTable_localdata)
    {
        FDMemTable_localdata->First();

        int cnt = FDMemTable_localdata->RecordCount;

        FuelPumpForm->addLog("Map points: "+IntToStr(cnt));

        FuelPumpForm->addLog("Map maxpts: "+IntToStr(FuelPumpForm->globaldata.maxbadgeview));

        if (cnt>FuelPumpForm->globaldata.maxbadgeview) cnt=FuelPumpForm->globaldata.maxbadgeview;

        FuelPumpForm->addLog("Map counts: "+IntToStr(cnt));

        for (int i = 0; i<cnt; i++)
        {
            FuelPumpForm->addLog("Map iterator: "+IntToStr(i));
            try{
                midstring = "&markers=size:mid|color:";

                String s_rank;
                int rank;

                float since = FDMemTable_localdata->FieldByName("since")->AsFloat;

//              since = 0.0;

                s_rank = FDMemTable_localdata->
                    FieldByName("rank")->AsString;


                if(since<12.0)
                {
                    midstring += "green"; ;
                }
                else if ((since>=12.0)&&(since<36.0))
                {
                    midstring += "yellow";
                }
                else if ((since>=36.0)&&(since<168.0))
                {
                    midstring += "red";
                }
                else
                {
                    midstring += "gray";
                }



                try
                {


                    midstring += "|label:"+IntToStr(StrToInt(s_rank));
                }
                catch(...)
                {
                    midstring += "|label:"+s_rank;
                }

                midstring += "|";

                lat_string.sprintf(L"%.4f",FDMemTable_localdata->
                    FieldByName("latitude")->AsFloat);

                midstring+=lat_string+",";

                lon_string.sprintf(L"%.4f",FDMemTable_localdata->
                    FieldByName("longitude")->AsFloat);

                midstring+=lon_string;

            }
            catch(...)
            {
                midstring="";

                ShowMessage("Error in calculating map data");

                FDMemTable_localdata->Next();

                continue;
            }

            mapstring +=midstring;

            //FuelPumpForm->addLog(mapstring);

            FDMemTable_localdata->Next();
        }

    }
    mapstring +="&key="+mapdata.key;



    FormatSettings.DecimalSeparator = LDecSeparator;

    //webbrowser->Navigate(mapstring);
    DownloadMap(mapstring);

    FuelPumpForm->addLog("Map: "+mapstring);
}

//---------------------------------------------------------------------------

__fastcall void TViewMapForm::mapfocustrackchange(TObject *Sender)
{
    updateMap();
}

//---------------------------------------------------------------------------

__fastcall void TViewMapForm::btncloseclick(TObject *Sender)
{
    this->Visible=false;
}

//---------------------------------------------------------------------------

__fastcall void TViewMapForm::btnrefreshclick(TObject *Sender)
{

    btn_pos_refresh->IconTintColor=TAlphaColor(claYellow);
    //locationsensor->Active = true;
    mapdata.longitude = FuelPumpForm->globaldata.mylongitude;
    mapdata.latitude = FuelPumpForm->globaldata.mylatitude;
    AnsiString as_lattext, as_lontext;

    as_lattext.sprintf("Latitude:\n%010.6f",
        mapdata.latitude);

    as_lontext.sprintf("Longitude:\n%010.6f",
        mapdata.longitude);

    lb_latitude->Text=as_lattext;

    lb_longitude->Text=as_lontext;

    updateMap();

}

1 个答案:

答案 0 :(得分:0)

对不起......我对静态地图的请求实际上使用了https://而不是http://。将其更改为http://很可能会解决问题。这将在我的下一个产品版本中完成。但它引入了一个安全风险。 ..