I want to show data of below JSON:
{"success":true,"message":"","result":{"Bid":3924.01000000,"Ask":3925.00000000,"Last":3924.00000000}}
I am new in android and from all tutorials, I somehow access this data but I want to show only "Last" data figure but my code shows whole "result" string.
String firstname;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.name);
new AsyncTaskParseJson().execute();
}
// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc";
// contacts JSONArray
JSONArray dataJsonArr = null;
protected void onPreExecute() {}
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JSONParser jParser = new JSONParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
firstname = json.getString("result");
Log.e(TAG, "Last: " + firstname);
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {
tv.setText(""+firstname);
}
}
You are not making a network call. You are just parsing your Url. You need to make network call. You can use volley for this. In your onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.name);
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject json = new JSONObject(response);
try {
JSONObject result = json.getJSONObject("result");
Double last = result.getDouble("Last");
tv.setText(String.valueOf(last));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
you need to define internet permission in Manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
You need to include volley library for this http request. Include this in build.gradle of your app
dependencies {
.
.
compile 'com.android.volley:volley:1.0.0'
.
.
}
I think you should be able to do this
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
return "" + json.get("result").getDouble("Last");
The string is returned from doInBackground to onPostExecute in the strFromDoInBg variable, but if you prefer:
firstname = "" + json.get("result").getDouble("Last");
would also work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.