Goodnight. I wrote a PHP script that outputs the data in the format "0.0 0.0 name" (where 0.0 is the X and Y coordinates, respectively, and name is the name). These are coordinates of architectural objects to which I need to switch randomly the initial position of Google Maps. The script works fine: it displays everything correctly, if you go directly to the link of its location. However, my code in Android Studio does not accept this data. Tell me, please, what could be the problem:

public class GetMapPlace extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public String getMapPlace() { String line = "0.0 0.0 none"; try { URL url = new URL(URL_GET_MAP); // подключаемся к интернету URLConnection connect = url.openConnection(); connect.setDoOutput(true); BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream())); // включаем буфер стандартного вывода, занося все в него (echo) StringBuilder builder = new StringBuilder(); if (reader.readLine() != null) { builder.append(line); // получаем строку } Log.d("DEBUG", "ОТВЕТ" + line); // эта строка не отображается в логах } catch (Exception e) { e.printStackTrace(); } return line; } } 

In addition to the absence of the DEBUG line in the logs, I tried to display the line value manually: it is always equal to "0.0 0.0 none".

Here's how I use this data in the future:

  GetMapPlace activity = new GetMapPlace(); try { String line = activity.getMapPlace(); Log.d("DEBUG", "ERROR123" + " " + line); String[] words = line.split(" "); position1 = Float.parseFloat(words[0]); position2 = Float.parseFloat(words[1]); name = words[3]; } catch (Exception e) { e.printStackTrace(); } // For dropping a marker at a point on the Map LatLng sydney = new LatLng(position1, position2); googleMap.addMarker(new MarkerOptions().position(sydney).title(name).snippet("Marker Description")); // For zooming automatically to the location of the marker CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build(); googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

I will be glad to any help! Happy New Year, gentlemen!

  • one
    Happy New 2019m! At a minimum, an Internet connection must be in a separate stream. - Streletz
  • I apologize, but how to do it right? Or .. Maybe there is an example of any? - odosenok
  • one
    Documentation. Google Link to the 1st page of metanit.com/java/android/15.1.php (with explanation). - Streletz

2 answers 2

If I understand correctly, then your line is initialized once at the beginning of the function ( String line = "0.0 0.0 none"; ) and does not change later. Apparently, it is necessary to do something like this:

 if ((line = reader.readLine()) != null) { builder.append(line); // получаем строку } ... return builder.toString(); 

Well, about the lack of DEBUG in the logs - you need to look at the settings of the logger (does any debug get into the logs at all?)

  • Tpak I have it ... - odosenok
  • one
    you have if (reader.readLine() != null) (i.e., the line is read, but not assigned to a local variable). and you return a local variable from the function (apparently, believing that it should be changed during data reading). - kami
  • one
    it is possible to completely rewrite the fragment of data reading and returning the result. something like this: return Optional.ofNullable(reader.readLine()).orElse("0.0 0.0 none"); - kami
  • Unfortunately, this method did not help me ... - odosenok

If I understand you correctly, you want to take part of the code from a page on the Internet. To do this, you need to connect the jsoup.jar library and then you need to write something like this code:

 try { Document doc = Jsoup.connect("https://ru.stackoverflow.com").get(); Elements element = doc.select("head"); String str = ""; for (org.jsoup.nodes.Element el : element) { parm = el.attr("title"); } }catch(MalformedURLException e){ Log.d("Veb", "doInBackground: error 1"); } 

And do not forget to open all this in a new stream.