Dsk
Dsk

Reputation: 37

Android: get and parse json data into a list view

I am currently working on an android project, and I want to be able to press a button and it will display user data into a listview on another activity. I currently have it working in a sense however it requires me to press two buttons. One gets the json data from the mysql database, and the other then sends it in an intent to the next activity allowing me to display it.

I was hoping there would be a way where both of these methods could be run by one button click, or else have both in one method.

public class MyMainActivity extends AppCompatActivity {


String url = "http://192.168.20.120";
String json_string;
View v;

//Button to get the data
ImageButton getUserDetailsBtn;

//Button to parse the data
ImageButton parseUserDetailsBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_main);

    getUserDetailsBtn= (ImageButton)findViewById(R.id.getUserDetailsButton);        

    parseUserDetailsBtn = (ImageButton)findViewById(R.id.parseUserDetailsButton);   

    getUserDetailsBtn.setVisibility(View.GONE); 


}


//
//get user details in app
//
public void getDetails(View view)  {

    new LoadUserDetails().execute();



    parseUserDetailsBtn .setVisibility(View.GONE);
    getUserDetailsBtn= .setVisibility(View.VISIBLE);



}




//Load the user details
class LoadUserDetails extends AsyncTask<Void,Void,String>
{

    String json_url;
    String JSON_STRING;

    @Override
    protected void onPreExecute() {

        json_url = url+"/getUserDetails.php";
    }

    @Override
    protected String doInBackground(Void... voids){

        try{

            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();


            while((JSON_STRING = bufferedReader.readLine())!=null)
            {

                stringBuilder.append(JSON_STRING+"\n");
            }

            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();

        } catch (MalformedJsonException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }



        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }


    @Override
    protected void onPostExecute(String result) {
        TextView textview = (TextView) findViewById(R.id.textview);
        textview.setText("");
        json_string = result;

    }
}




//
//parse the data
//
public void parseJSON(View view)
{

    if(json_string==null){
        Toast.makeText(getApplicationContext(), "First get JSON Data", Toast.LENGTH_LONG).show();

    }
    else{
        Intent intent = new Intent(this, ShowUserActivity.class);
        intent.putExtra("json_data", json_string);
        startActivity(intent);

    }


}

Upvotes: 2

Views: 44

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Start your Activity from onPostExecute because onPostExecute gets executed on UI thread

  @Override
    protected void onPostExecute(String result) {
        TextView textview = (TextView) findViewById(R.id.textview);
        textview.setText("");
        json_string = result;
        if(json_string==null){
            Toast.makeText(getApplicationContext(), "First get JSON Data",     Toast.LENGTH_LONG).show();    
        }
        else{
            Intent intent = new Intent(MyMainActivity.this, ShowUserActivity.class);
            // and change this to MyMainActivity.this
            // this will point to your task instead of Activity
            intent.putExtra("json_data", json_string);
            startActivity(intent);    
        }    
    }

Upvotes: 3

Related Questions