0

I want to show this JSON in a TextView in my Activity.

This is my JSON,

{
    "AboutUs": [{
        "p_content": "Dr. Vineet Malhotra currently working as a Consultant urologist where job description requires to run the urology units at these hospitals and perform various endourology, reconstructive and laparoscopic procedures. Dr. Vineet Malhotra associated with the department of urology for the last ten years and have been exposed to different practice methodologies.Dr. Vineet Malhotra have a special interest in recent advances in minimally invasive procedures in urology.\n"
    }]
}

And this is my Activity where I want to display this JSON in TextView,

public class AboutUs extends AppCompatActivity {
    private String TAG=MainActivity.class.getSimpleName();
    private ProgressDialog pDialog;

    TextView tv;
    private static String url="http://softappsdemo.com/doctorplus/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        new GetContacts().execute();
    }


    private class GetContacts extends AsyncTask<Void,Void,Void> {
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog=new ProgressDialog(AboutUs.this);
            pDialog.setMessage("Please Wait ......");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected Void doInBackground(Void... arg){
            Handler sh=new Handler();
            String jsonStr=sh.makeServiceCall(url);
            Log.e(TAG,"Response from url : " +jsonStr);
            if(jsonStr!=null){

                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray contacts = jsonObj.getJSONArray("AboutUs");

                        JSONObject c = contacts.getJSONObject(0);

                        String name = c.getString("p_content");




                }catch (final JSONException e){
                    Log.e(TAG,"Json parsing error: " +e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"JSON parsing error: " +e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    });
                }

            }else {
                Log.e(TAG,"Could not get JSON from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),"could not get JSOM from server check logcat if possible " ,Toast.LENGTH_LONG).show();
                    }
                });
            }

            return null;
        }
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();
        }
    }
}

Where I should have to put the code for TextView?

Please help me guys. Thanks in advance.

5
  • 1
    where are you setting the TextView text? Commented Sep 28, 2016 at 12:43
  • Take your String name variable as global and in onPostExecute method set the content in TextView. Commented Sep 28, 2016 at 12:44
  • I just want to knows where i set that ? Commented Sep 28, 2016 at 12:44
  • Wow thanks you very much grlsHu Commented Sep 28, 2016 at 12:50
  • @champandorid Welcome I have posted my answer please mark it correct if it helped you. Commented Sep 28, 2016 at 12:52

5 Answers 5

1

One way is to take your String name variable as global and in onPostExecute method set the content in TextView.

public class AboutUs extends AppCompatActivity {
    private String name;

.............................
..........................


 protected void onPostExecute(String result){
        super.onPostExecute(result);
        if (result != null)
            myTextView.setText(name);

    }
Sign up to request clarification or add additional context in comments.

Comments

0

Change your AsyncTask to the following:

private class GetContacts extends AsyncTask<Void,Void,String> {
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog=new ProgressDialog(AboutUs.this);
        pDialog.setMessage("Please Wait ......");
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(Void... arg){
        Handler sh=new Handler();
        String jsonStr=sh.makeServiceCall(url);
        Log.e(TAG,"Response from url : " +jsonStr);
        if(jsonStr!=null){

            try {

                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONArray contacts = jsonObj.getJSONArray("AboutUs");

                    JSONObject c = contacts.getJSONObject(0);

                    String name = c.getString("p_content");

                    return name;


            }catch (final JSONException e){
                Log.e(TAG,"Json parsing error: " +e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),"JSON parsing error: " +e.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });
            }

        }else {
            Log.e(TAG,"Could not get JSON from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),"could not get JSOM from server check logcat if possible " ,Toast.LENGTH_LONG).show();
                }
            });
        }




        return null;
    }
    protected void onPostExecute(String result){
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        if (result != null)
            tv.setText(result);

    }
} 

Comments

0
package androidthirst.company.abhi.navugation;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class AboutUs extends AppCompatActivity {
    private String TAG=MainActivity.class.getSimpleName();
    private ProgressDialog pDialog;

    TextView tv;
    private static String url="http://softappsdemo.com/doctorplus/";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        new GetContacts().execute();
    }


    private class GetContacts extends AsyncTask<Void,Void,String> {
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog=new ProgressDialog(AboutUs.this);
            pDialog.setMessage("Please Wait ......");
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected String doInBackground(Void... arg){
            Handler sh=new Handler();
            String jsonStr=sh.makeServiceCall(url);
            Log.e(TAG,"Response from url : " +jsonStr);
            if(jsonStr!=null){

                try {

                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray contacts = jsonObj.getJSONArray("AboutUs");

                        JSONObject c = contacts.getJSONObject(0);

                        String name = c.getString("p_content");
                        return name;
                }catch (final JSONException e){
                    Log.e(TAG,"Json parsing error: " +e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"JSON parsing error: " +e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    });
                }

            }else {
                Log.e(TAG,"Could not get JSON from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),"could not get JSOM from server check logcat if possible " ,Toast.LENGTH_LONG).show();
                    }
                });
            }




            return null;
        }
        protected void onPostExecute(String result){
            super.onPostExecute(result);
            if (pDialog.isShowing())
                pDialog.dismiss();
            tv.setText(result);

        }
    }
}

Comments

0

1.add tv = (TextView)findViewById(R.id.txt);//add this line in your onCreate method before

new GetContacts().execute(); line.
  1. change
    private class GetContacts extends AsyncTask<Void,Void,Void>

    with

    private class GetContacts extends AsyncTask<String,Void,Void>

    because you need String in result to display in TextView

  2. return name; instead of null in doInBackground method

  3. protected void onPostExecute(Void result){ replace Void in parameter with String because we are going to get result in String.
  4. add tv.setText(result); after super.onPostExecute(result); in onPostExecute method.

Comments

0
public class MainActivity extends AppCompatActivity {

TextView tv = null;
InputStream is = null;
public static final String URL = "http://www.projectconnect.com.br/transportefacil/api/pedido/list/";
String json;
HttpResponse httpResponse = null;

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

    tv = (TextView) findViewById(R.id.tv_json);

    Task task = new Task();
    task.execute();
}

private class Task extends AsyncTask<Bundle, Void, String> {

    protected String doInBackground(Bundle... parametros) {

        HttpGet httpGet = new HttpGet(URL);

        httpGet.setHeader("Content-type", "application/json");
        httpGet.setHeader("Accept-Encoding", "compress, gzip");
        httpGet.setHeader("Accept", "application/json");

        DefaultHttpClient httpClient = new DefaultHttpClient();

        try {
            httpResponse = httpClient.execute(httpGet);

            is = AndroidHttpClient.getUngzippedContent(httpResponse.getEntity());

            json = Util.converteGzipEmJson(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    protected void onPostExecute(String json) {
        tv.setText(json);
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.