I need to use this api :
I wrote the following code:
public class JsonResponse { HttpURLConnection urlConnection ; BufferedReader reader ; String resultJson ; String strJson; private String phone; private String address; private String name; public String get() { try { URL url = new URL("http://search-maps.yandex.ru/v1/?text=Отель&lang=ru_RU&apikey=тут мой Api key"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return resultJson;} protected void onPostExecute(String strJson) { onPostExecute(strJson); JSONObject dataJsonObj; String Name; try { dataJsonObj = new JSONObject(strJson); JSONArray objects = dataJsonObj.getJSONArray("features"); JSONObject meta = objects.getJSONObject(Integer.parseInt("CompanyMetaData")); JSONObject phones = meta.getJSONObject("CompanyMetaData.Phones"); String name = meta.getString("name"); String address = meta.getString("address"); String phone = phones.getString("formatted"); } catch (JSONException e) { e.printStackTrace(); } } public String getPhone() { return phone; } public String getName() { return name; } public String getAddress() { return address; } public String getStrJson() { return strJson; } } Call to main activity:
JsonResponse json = new JsonResponse(); Log.d(TAG,"Json:" + json.getStrJson() ); Log.d(TAG,"JsonName:" + json.getName() ); Log.d(TAG,"JsonPhone:" + json.getPhone() ); Log.d(TAG,"JsonAddress:" + json.getAddress() ); The logs are null.
null. And the request to the network must be done from a non-UI stream. - Yuriy SPb ♦get()you want to see the values :) - Eugene Krivenja