|
| 1 | +package sayan.example.com.ionnetconnectorsample; |
| 2 | + |
| 3 | +import android.app.ProgressDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.util.Log; |
| 8 | +import android.widget.TextView; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | +import com.google.gson.Gson; |
| 12 | +import com.google.gson.JsonArray; |
| 13 | +import com.google.gson.JsonObject; |
| 14 | +import com.google.gson.reflect.TypeToken; |
| 15 | +import com.koushikdutta.async.future.Future; |
| 16 | +import com.koushikdutta.async.future.FutureCallback; |
| 17 | +import com.koushikdutta.ion.Ion; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.concurrent.ExecutionException; |
| 21 | + |
| 22 | +import sayan.example.com.ionnetconnectorsample.pojos.PhoneNumber; |
| 23 | +import sayan.example.com.ionnetconnectorsample.pojos.UserAddress; |
| 24 | +import sayan.example.com.ionnetconnectorsample.pojos.UserDetails; |
| 25 | + |
| 26 | +public class GetStringJSONData extends AppCompatActivity { |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void onCreate(Bundle savedInstanceState) { |
| 30 | + super.onCreate(savedInstanceState); |
| 31 | + String result = null; |
| 32 | + setContentView(R.layout.activity_get_jsondata); |
| 33 | + String url = "https://api.myjson.com/bins/imon9"; |
| 34 | + try { |
| 35 | + result = getJSON(this, url, createProgressDialog()); |
| 36 | + } catch (ExecutionException | InterruptedException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + //create the progressDialog object |
| 42 | + private ProgressDialog createProgressDialog(){ |
| 43 | + final ProgressDialog dlg = new ProgressDialog(this); |
| 44 | + dlg.setTitle("Loading..."); |
| 45 | + dlg.setIndeterminate(false); //indeterminate= circular progress |
| 46 | + dlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
| 47 | + return dlg; |
| 48 | + } |
| 49 | + |
| 50 | + private String getJSON(final Context context, String url, final ProgressDialog dialog) throws ExecutionException, InterruptedException { |
| 51 | + dialog.show(); |
| 52 | + Future<String> resultJSON = Ion.with(context) |
| 53 | + .load(url) |
| 54 | + .progressDialog(dialog) |
| 55 | + .setLogging("sayanJson", Log.DEBUG) |
| 56 | + .asString() |
| 57 | + .setCallback(new FutureCallback<String>() { |
| 58 | + @Override |
| 59 | + public void onCompleted(Exception e, String result) { |
| 60 | + Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); |
| 61 | + if (dialog.isShowing()){ |
| 62 | + dialog.dismiss(); |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + return resultJSON.get(); |
| 67 | + } |
| 68 | +} |
0 commit comments