I created a new class to connect to the server (Where Json is located) using AsyncTask, I need to create a button that, when clicked, goes to the link. I tried to withdraw the array, but it did not help.

Here is the code MainActiviy.java

package my.home.page; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import org.json.JSONException; import my.home.page.classss.ParseTask; public class MainActivity extends Activity { public static String LOG_TAG = "my_log"; String Links, names; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView list = (ListView) findViewById(R.id.list); String str = null; try { str = this.getJSON(); } catch (JSONException e) { e.printStackTrace(); } System.out.println(str); } private String getJSON() throws JSONException { ParseTask pt = new ParseTask(); pt.execute(); return null; } } 

This is what Json looks like on the server.

 [{"id":1,"link":"www.vk.com","name":"VK"}, {"id":1,"link":"www.google.com","name":"google"}, {"id":1,"link":"www.ya.ru","name":"Yandex"}] 

2 answers 2

First look at your code.

  • In activation, you are trying to get a string using the getJSON method
  • Let's see what the getJSON method returns: it returns null

It is logical that you do not see anything in the output

In general, you should better study android and AsyncTask in particular:

  • In the android in the tele activity you can not directly access the network
  • The main thread is responsible for the UI and you can not slow it down by heavy processes, which is access to the network
  • For heavy processes and access to the network in particular, you can use AsyncTask - tasks that are performed in a separate thread
  • Logging can be done directly in the doInBackground method after receiving data from the network.
  • To access the UI in AsyncTask, you can override the onPostExecute method that runs after the job and is used to update the interface
  • The onPostExecute method gets the data that you should have received in the doInBackground method
  • To do this, you must return this data using the return construct in the doInBackground method

More details:

  • Well, I have AsyncTask in a separate thread \ - Shufler3
  • and as I understood, onPostExecute to write in one thread with doInBackground? - Shufler3
  • You do not need to call the onPostExecute method yourself. - Michael Rebrov
  • You only need to override it. - Michael Rebrov
  • It is called in the execute method after calling the doInBackground method - Mikhail Rebrov

Better use the Retrofit library. Here is a link to an example.

  • one
    Retrofit is good! But first we need knowledge and then additional libraries - elik