Answer the question
In order to leave comments, you need to log in
Working with JSON by reference. How to get objects one by one (shortcode)?
Good afternoon!
As a beginner, I'm stuck a bit with Json parsing by reference.
The crux of the matter is this.
You need to get exchange rates from this link https://api.privatbank.ua/p24api/exchange_rates?js... I
wrote this code.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class JSONParser extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
String url = "https://api.privatbank.ua/p24api/exchange_rates?json&date=01.12.2014";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) throws JSONException {
JSONParser jsonParser = new JSONParser();
JSONObject object = jsonParser.getJSONFromUrl(url);
String content=object.getString("currency");
// String content1 = getJSONFromUrl(url).getString("date");
// if (content1.equals(object)){
// JSONArray arrayClentHistoryRequest = getJSONFromUrl(url).getJSONArray("exchangeRate");
// for (int i=0; i<arrayClentHistoryRequest .length();i++)
// {
// Toast.makeText(arrayClentHistoryRequest.get(i).getString("currency"), Toast.LENGTH_LONG).show();
// }
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
System.out.println(line);
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
System.out.println("error on parse data in jsonparser.java");
}
return jObj;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question