Select to view content in your preferred language

Argcis Android AsyncTask With UserCredentials

5080
6
Jump to solution
08-18-2014 11:34 PM
GorkemKaradogan
New Contributor

My problem is that i want to get json data from Arcgis Server For example:http://xxxx.com/arcgis/rest/services/Map/MapServer/legend?f=pjson i can get and read json data from this url but if arcgis server has a password i couldn't so , must use UserCredentials but every sample example use UserCredentials for identity task or querytask but i read data from url. how can i post UserCredentials with my AsyncTask ?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanO_Neill
Occasional Contributor III

We don't support those endpoints in the API so you have to honor the REST API and parse the JSON.  Secured service endpoints will require you to authenticate on the URL request not with UserCredentials.  URL will be in the form of:

http://myserver/ArcGIS/rest/services/folder/service/MapServer?token=mytoken./legend

You can find an example of creating a Legend in the Android API here

View solution in original post

6 Replies
DanO_Neill
Occasional Contributor III

You can connect to Feature Layers outside of AsyncTask by passing the credentials with the ArcGISFeatureLayer constructor.  You can set the credentials in the AsyncTask.doInBackground() method for background tasks that require user credentials, e.g. query a secured feature layer.  You can refer to the Secured Feature Layer SDK sample to see examples of both patterns. 

0 Kudos
GorkemKaradogan
New Contributor

but in thats examples asynctask will return feature set but i need to wait string for example i will get legend or coded value from secured arcgis service how i can i get that ?  I have to use async task because getlegend() on layer is not run correctly and attribute add or show arcgis sdk examples not understand item has codedvalue.

0 Kudos
DanO_Neill
Occasional Contributor III

What are you trying to do?  Android developers have to use symbology definitions to build a legend as you cannot use the endpoint you have referenced.  If you are trying to parse json outside of the Android API you have to follow the REST API security model, which means you need a token to make the request through a URL in an AsyncTask.  You can authenticate with an Android FeatureLayer, get the token out for your user credential and append to your legend request.  Is that what you are trying to do?  I can share sample code on how to create a Legend if that is what you are trying to do. 

0 Kudos
GorkemKaradogan
New Contributor
ArcGISLayerInfo[] layers = c.getMap().getLayers(); // It gives me my tiledmap.
for (int i = 0; i < layers.length; i++) {
List<Legend> _legend = layers.getLegend();
Log.v("Legend :", _legend.get(0).getLabel());
}
But this code is not working so i wrote codes for reading legends these codes. 
private class LegandBul extends
               AsyncTask<String, Void, ArrayList<ArrayList<legendClass>>> {
          String url;
          // contacts JSONArray
          ArrayList<legendClass> legendA;
          ArrayList<ArrayList<legendClass>> layersA;

          @Override
          protected void onPreExecute() {
               super.onPreExecute();
               // Showing progress dialog

               legendA = new ArrayList<legendClass>();
               layersA = new ArrayList<ArrayList<legendClass>>();
          }

          @Override
          protected ArrayList<ArrayList<legendClass>> doInBackground(
                    String... query) {
               // Creating service handler class instance
               url = query[0];
               ServiceHandler sh = new ServiceHandler();

               // String json = new
               // GsonBuilder().create().toJson(anaEkraninstance.creds,
               // UserCredentials.class);

               // Making a request to url and getting response
               String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

               if (jsonStr != null) {
                    try {
                         JSONObject jsonObj = new JSONObject(jsonStr);

                         // Getting JSON Array node
                         JSONArray layers = jsonObj.getJSONArray("layers");

                         // looping through All Contacts
                         for (int i = 0; i < layers.length(); i++) {

                              JSONObject c = layers.getJSONObject(i);
                              JSONArray legend = c.getJSONArray("legend");

                              for (int j = 0; j < legend.length(); j++) {
                                   JSONObject legendic = legend.getJSONObject(j);
                                   String label;
                                   if (legendic.getString("label").isEmpty()) {
                                        label = c.getString("layerName");
                                   } else {
                                        label = legendic.getString("label");
                                   }

                                   String imageData = legendic.getString("imageData");

                                   legendClass newLegend = new legendClass(label,
                                             StringToBitMap(imageData));
                                   legendA.add(newLegend);

                              }

                         }
                         layersA.add(legendA);
                         return layersA;
                    } catch (JSONException e) {
                         e.printStackTrace();
                    }
               } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
               }

               return null;
          }

          @Override
          protected void onPostExecute(ArrayList<ArrayList<legendClass>> result) {
               super.onPostExecute(result);

               for (int i = 0; i < layersA.size(); i++) {

                    for (int j = 0; j < layersA.get(i).size(); j++) {

                         resultLegend.add(layersA.get(i).get(j));
                    }
               }
               legendAdapter.setItemList(resultLegend);
               legendAdapter.notifyDataSetChanged();

               /**
                * Updating parsed JSON data into ListView
                * */

          }

     }
 
I can read legends with these codes. it run correctly and show me legends of tiledmap.But if my map has control for login, it needs to password or username like usercredentials so how can i implement this asynctask running with usercredentials. 
Also i don't want to learn that for only getting legends list. For example. When i edit feature layer and if attribute has a domain, esri android sdk sample codes not getting codedvalues so i wrote code for it and if attribute has a domain i get codedvalue with asynctask so again i must use asynctask with usercredentials. I hope you understand, my english is not good, i am sorry about that. If you have sample code about Async Task with UserCreditials, Can you share with me ? 
0 Kudos
DanO_Neill
Occasional Contributor III

We don't support those endpoints in the API so you have to honor the REST API and parse the JSON.  Secured service endpoints will require you to authenticate on the URL request not with UserCredentials.  URL will be in the form of:

http://myserver/ArcGIS/rest/services/folder/service/MapServer?token=mytoken./legend

You can find an example of creating a Legend in the Android API here

GorkemKaradogan
New Contributor

Thanks so much, it's helpfull form situation.

0 Kudos