Reputation: 1458
I am having very simple JSON format data from here and it looks like this:
{
"date" : "2013-11-01",
"gold" : "1317.29",
"silver" : "21.90",
"platinum" : "1458.00"
}
I only need to get the value of "gold" tag.
My code is:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class JsonActivity extends Activity {
static String jsonUrl = "http://services.packetizer.com/spotprices/?f=json";
static String gold = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
new MyAsyncTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.json, menu);
return true;
}
private class MyAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
DefaultHttpClient httpCl = new DefaultHttpClient(new
BasicHttpParams());
HttpPost httpP = new HttpPost(jsonUrl);
httpP.setHeader("Content-type", "application/json");
InputStream in = null;
String result = null;
try {
HttpResponse response = httpCl.execute(httpP);
HttpEntity entity = response.getEntity();
in = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader(in, "UTF-8"), 8);
StringBuilder sbuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sbuilder.append(line + "\n");
}
result = sbuilder.toString();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(in != null)
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
JSONObject jsonObj;
try {
jsonObj = new JSONObject(result);
gold = jsonObj.getString("gold");
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
TextView goldTV = (TextView)findViewById(R.id.goldPrice);
goldTV.setText("good price with Json" + gold);
super.onPostExecute(result);
}
}
}
however, when I run it the String gold
is returning always null
. I could not figure it out why? Can anyone tell me what went wrong?
thanks
Update A screenshot is added:
Upvotes: 0
Views: 765
Reputation: 110
check if your jsonObj is null or not,if it is null perhaps you should use something like this:
JSONArray jsonArray;
JSONObject parent;
try {
jsonArray = new JSONArray(result);
parent = jsonArray.getJsonObject("ParentObject");
gold = parent.getString("gold");
}
catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 17441
jus replace this code with your code
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class JsonActivity extends Activity {
static String jsonUrl = "http://services.packetizer.com/spotprices/?f=json";
static String gold = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
new MyAsyncTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.json, menu);
return true;
}
private class MyAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
DefaultHttpClient httpCl = new DefaultHttpClient(new
BasicHttpParams());
HttpPost httpP = new HttpPost(jsonUrl);
httpP.setHeader("Content-type", "application/json");
InputStream in = null;
String result = null;
try {
HttpResponse response = httpCl.execute(httpP);
HttpEntity entity = response.getEntity();
in = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader(in, "UTF-8"), 8);
StringBuilder sbuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sbuilder.append(line + "\n");
}
result = sbuilder.toString();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(in != null)
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
if(!TextUtils.isEmpty(result))
{
JSONObject jsonObj;
try {
jsonObj = new JSONObject(result);
gold = jsonObj.getString("gold");
}
catch (JSONException e) {
e.printStackTrace();
}
TextView goldTV = (TextView)findViewById(R.id.goldPrice);
goldTV.setText("good price with Json" + gold);
}
else
{
Toast.makeText(this,"response is null",Toast.LENGTH_LONG);
}
super.onPostExecute(result);
}
}
}
Upvotes: 1
Reputation: 5295
You are doing jsonObj.getString("gold");
.. now you are trying to get a string from this json Object, but in your json, there is no main parent object, like for example :-
{
"Android" :
[
{
"date" : "2013-11-01",
"gold" : "1317.29",
"silver" : "21.90",
"platinum" : "1458.00"
}
]
}
Here Android is your jsonobject and when you call getstring method, then, you will get the value of gold in a string variable. Go through these two tutorials, you will understand :-
Upvotes: 1