Reputation: 61
My json data looks like -
{
android: [
{
name: "link1",
link: "href="/downloads/-------------""
},
{
name: "link2",
link: "http://www.----------"
}
]
}
I have to dispaly ONLY NAMES fetched from json data on my activity ,in such a way that when names are displayed it should be clickable and on clicking that name it should redirect it to the respective links.
**For Example : **
When NAME(link1) is fetched from json data and is clicked, it should redirect it to the respective link "href="/downloads/-----------"
.
I am facing difficulty in making names clickable to their respective links.
Upvotes: 0
Views: 92
Reputation: 5904
Referring to this: How to parse JSON in Android you need a JSONParser class for first:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
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");
}
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());
}
// return JSON String
return jObj;
}
}
Then declare all variables you need in your activity:
// url to make request private static String url = "http://www.yourjsonurl.com/json.php";
// JSON Node names private static final String TAG_CONTACTS = "contacts"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "name";
// contacts JSONArray JSONArray contacts = null;
and then:
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
}
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 1768
HashMap<String, String> namelink = new HashMap<String, String>();
JsonArray root=response.getJsonArray("android");
for(int i=0;i<root.size();i++)
{
JsonObject innerElement=root.getJsonObject(i);
String name=innerElement.getString("name");
String link=innerElement.getString("link");
namelink .put(name, link);
}
now ..the hashmap "namelink",contain name with link...get hashmap value (link) according to name
for eg;
String linkretreived=namelinke.get(<key>);
Upvotes: 0
Reputation: 490
JsonArray root=response.getJsonArray("Android");
for(int i=0;i<root.size();i++)
{
JsonObject innerElement=root.getJsonObject(i);
String name=innerElement.getString("name");
String name=innerElement.getString("link");
}
Upvotes: 1