Asked  7 Months ago    Answers:  5   Viewed   43 times

As a learning experience, I want to make an iPhone application that calls a webserver/webservice, retrieves a JSON response, and uses that response to populate the rows of a UITableView (assuming it converts the JSON into an NSArray first).

Anyone know of anything that might be useful?

 Answers

82

You will love this framework.

And you will love this tool.

For learning about JSON you might like this resource.

And you'll probably love this tutorial.

Tuesday, June 1, 2021
 
Joegramming
answered 7 Months ago
37

Here is my code for parsing json from an URL :

public JSONObject getJSONFromUrl(String url) {
try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    json = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
JSONObject jObj;
// 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());
}
Log.v("debug", "JSON ready to parsing");
return jObj;
}

public void parsingData(JSONObject json) {
try {
    JSONArray data = json.getJSONArray("data");
    for (int i = 0; i < data.length(); i++) {
        // Do your stuff, example :
        JSONObject c = data.getJSONObject(i);
        JSONObject topDrops = c.getJSONObject("topDrops");
        JSONArray playerArray = topDrops.getJSONArray("player");
        // playerArray.getJSONObject(0) == first player
    }
} catch (JSONException e) {
    e.printStackTrace();
    Log.v("debug", "Error during the connection HTTP");
    cancel(Boolean.TRUE);
}
}
Friday, August 6, 2021
 
diegoiglesias
answered 4 Months ago
93

Try this..

JSONArray jsonarray = new JSONArray(json);

for (int i = 0; i < jsonarray.length(); i++) {

       JSONObject jsonobj = jsonarray.getJSONObject(i);

      System.out.println("categoryId : " + i + " = " + jsonobj.getString("categoryId"));
       System.out.println("Title : " + i + " = " + jsonobj.getString("Title"));
       System.out.println("songs : " + i + " = " + jsonobj.getString("songs"));
}
Saturday, August 7, 2021
 
aurelijusv
answered 4 Months ago
47

It seems your tableView's frame height is exceeding the visible area. Decrease the tableView's frame height to be inside the visible area. You will see everything.

Sunday, September 19, 2021
 
iteong
answered 3 Months ago
76

Ok.. You do not create IBOutlet connection from File's Owner. Have a look at a screenshot. You create IBOutlet from CustomCell's view(with Red Arrow).

Looking after your code just follow these steps.

1) Goto CustomCell.h file. As you are saying customCell.xib has two UILabels(assume label1 & label2) you gonna have to declare properties and create outlets in CustomCell.h file and in .m file synthesize and release it. Refer this code screen of mine.

enter image description here

2) Now in CustomCell.xib, select view of CustomCell not File's Owner(File's Owner should inherit from NSObjectonly) go to Identity Inspector(Marked with Red Ellipse) and select the corresponding Customcell class (marked with Red rectangle).

3) Right click your customcell's view and make connections to labels. And save it..

enter image description here

4) In your DirectionsViewController.m you have this UITableView's delegate method cellForRowAtIndexPath. Change it like this :

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CustomCellIdentifier = @"CustomCell";
EditProjectCustomCell *cell = (EditProjectCustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; // typecast to customcell

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

if (cell == nil)
{ 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EditProjectCustomCell" owner:self options:nil];

        for (id oneObject in nib) 
            if ([oneObject isKindOfClass:[EditProjectCustomCell class]])

            cell = (EditProjectCustomCell *)oneObject;

        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

}

cell.label1.text=[someArray1 ObjectAtIndexPath: indexPath.row];
cell.label2.text=[someArray2 ObjectAtIndexPath: indexPath.row];

return cell;

}

this delegate method is gonna be called as many times as you returned value in numberOfRowsInSection DataSource method. Every time cell.label will be blank and you will add data to label by calling this line. so no need to create label each time as you did between line 79-90 here. http://pastebin.com/Vd4PKjTu

 cell.label1.text=[someArray ObjectAtIndexPath: indexPath.row];

Creating custom cell means you create UI(i.e. .xib),interface & implementation (.h,.m file) for UITableViewCell by yourself and adopt them in your class's (i.e. DirectionsViewController.m) cellForRowAtIndexPath delegate method.

Saturday, November 20, 2021
 
lupz
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share