0

I am building an Android application that receives some data in JSON format. At the moment the result is very ugly. I want to display this data neatly in multiple TextViews in my application but I am not to sure how to do it. Source code below.

Main.java

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {

    TextView txt1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button)findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> {

        protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
           InputStream in = entity.getContent();
             StringBuffer out = new StringBuffer();
             int n = 1;
             while (n>0) {
                 byte[] b = new byte[4096];
                 n =  in.read(b);
                 if (n>0) out.append(new String(b, 0, n));
             }
             return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();
             HttpGet httpGet = new HttpGet("https://api.myjson.com/bins/363s3");
             String text = null;
             try {
                   HttpResponse response = httpClient.execute(httpGet, localContext);
                   HttpEntity entity = response.getEntity();
                   text = getASCIIContentFromEntity(entity);
             } catch (Exception e) {
                 return e.getLocalizedMessage();
             }
             return text;
        }   

        protected void onPostExecute(String results) {
            if (results!=null) {
                txt1 = (TextView)findViewById(R.id.textView1);
                txt1.setText(results);
            }
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }
}
  • What json string getting in response from server ? – ρяσѕρєя K Feb 28 '15 at 16:24
  • where is your `JSON` returned from server, and how have you parsed it? – Xcihnegn Feb 28 '15 at 17:18
  • this is whats returned [{"IndividualID":1,"FullName":"Sean Kelly","DOB":"07/06/1987","MedicalHistory":"Ulcerative Colitis","Medication":"Asacolon","Alergies":"Penicillin"}] would there be a simpler way of formatting the data like a json array? I want simple code just to output the data to textviews e.g Patient: 1, Fullname: Sean Kelly etc. –  Feb 28 '15 at 17:20

3 Answers3

0

What about trying something like,

JSONObject jsonObject = new JSONObject(jsonString);

By the way, Volley is a good library for any kind of requests.

Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
0

i use retrofit for asynchronous execution use Callback

gadfil
  • 417
  • 4
  • 13
0

If what u mean is how to structure the data u receive from the json then you should look into json formatting frameworks (like 'Jackson' or 'Gson' or others).

If what u mean is how to make the textual json appear neatly in a textbox, jackson also appears to have an option to print the json neatly (try this: http://www.mkyong.com/java/how-to-enable-pretty-print-json-output-jackson/ )

Subler
  • 657
  • 6
  • 11