-2

This line of code is giving me errors :

 HttpPost httppost = new HttpPost("file:///android_asset/www/me.json");

instead of file,even if i use "http://xyz......"); i still get errors ./....................
import android.os.Bundle; import android.app.Activity; import android.webkit.WebView; import android.widget.TextView;

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;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    TextView TV=(TextView)findViewById(R.id.tv1);
    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("file:///android_asset/www/me.json");

    InputStream inputStream = null;

    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();
        String result=null;
        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
        TV.setText(result);
    } catch (Exception e) { 
       Log.e("MYAPP", "exception", e);
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }
    }

}

and this is the json file

{"resp": {"status": true, "version": "2.0", "artist": {"profile": "An Australian rock band, formed in 1973 by Angus and Malcolm Young, they teamed up with Dave Evans (vocals), Larry Van Kriedt (bass) and Colin Burgess (drums). In 1974 both Larry Van Kriedt and Colin Burgess left and were replaced by Rob Bailey (bass) and Peter Clack (drums), a further change in 1974 saw Peter Clack leave and Tony Currenti (drums) join the band. In June 1974 they were signed by Harry Vanda & George Young (Malcolm & Angus's brother) to Albert Productions. In November 1974, Dave Evans left the band and was replaced by Bon Scott (vocals & bagpipes). Rob Bailey also left in 1974 and was replaced by George Young (bass). In 1975 Phil Rudd (drums) replaced Tony Currenti and Mark Evans (bass) replaced George Young.  In June 1977 Mark Evans left and is replaced by Cliff Williams (bass) for their first tour of the USA. On the 19 Feb 1980 Bon Scott died at the age of 33. Brian Johnson (ex Geordie) joined the band to replace him on vocals and the album \"Back In Black\" was released, a tribute to Bon Scott, this album became the 2nd largest selling album of all time with over 40 million copies sold worldwide. In May 1983, Phil Rudd had a parting of the ways and was replaced by Simon Wright (drums), aged 20 then. November 1989 Simon Wright left and is replaced by Chris Slade (ex Manfred Mann's Earth Band, Uriah Heep & The Firm). In summer 1994 Phil Rudd \"quietly\" rejoined the band.\r\n\r\nAC/DC are Australia's most successful rock band ever, and are popular around the world.\r\nThe band was inducted into Rock And Roll Hall Of Fame in 2003 as a performer.\r\n\r\nCurrent line-up:\r\nAngus Young (lead guitar)\r\nMalcolm Young (rhythm guitar)\r\nBrian Johnson (vocals)\r\nCliff Williams (bass guitar)\r\nPhil Rudd (drums)\n", "releases_url": "http://api.discogs.com/artists/84752/releases", "name": "AC/DC", "uri": "http://www.discogs.com/artist/AC%2FDC", "members": ["Angus Young", "Bon Scott", "Brian Johnson", "Chris Slade", "Cliff Williams", "Colin Burgess", "Dave Evans", "Larry Van Kriedt", "Malcolm Young", "Mark Evans (3)", "Phil Rudd", "Simon Wright (4)"], "urls": ["http://www.acdcrocks.com/", "http://www.acdc.com/", "http://www.acdcpower.net/", "http://www.myspace.com/acdc", "http://en.wikipedia.org/wiki/AC/DC"], "images": [{"uri": "http://api.discogs.com/image/A-84752-1233004620.jpeg", "height": 309, "width": 418, "resource_url": "http://api.discogs.com/image/A-84752-1233004620.jpeg", "type": "primary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004620.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1094915280.jpg", "height": 313, "width": 300, "resource_url": "http://api.discogs.com/image/A-84752-1094915280.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1094915280.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1105107816.jpg", "height": 129, "width": 180, "resource_url": "http://api.discogs.com/image/A-84752-1105107816.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1105107816.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1107645658.jpg", "height": 199, "width": 200, "resource_url": "http://api.discogs.com/image/A-84752-1107645658.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1107645658.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1182165014.jpeg", "height": 335, "width": 498, "resource_url": "http://api.discogs.com/image/A-84752-1182165014.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1182165014.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004627.jpeg", "height": 360, "width": 480, "resource_url": "http://api.discogs.com/image/A-84752-1233004627.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004627.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004633.jpeg", "height": 247, "width": 457, "resource_url": "http://api.discogs.com/image/A-84752-1233004633.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004633.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004641.jpeg", "height": 376, "width": 400, "resource_url": "http://api.discogs.com/image/A-84752-1233004641.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004641.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004648.jpeg", "height": 389, "width": 572, "resource_url": "http://api.discogs.com/image/A-84752-1233004648.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004648.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1297548279.jpeg", "height": 414, "width": 600, "resource_url": "http://api.discogs.com/image/A-84752-1297548279.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1297548279.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1294786426.jpeg", "height": 450, "width": 450, "resource_url": "http://api.discogs.com/image/A-84752-1294786426.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1294786426.jpeg"}], "resource_url": "http://api.discogs.com/artists/84752", "id": 84752, "data_quality": "Correct", "namevariations": ["AC / DC", "AC DC", "AC-DC", "AC//DC", "ACDC", "DC/AC", "\u042d\u0439 \u0421\u0438 \u0414\u0438 \u0421\u0438"]}}}
Ankit Srivastava
  • 280
  • 2
  • 7
  • 24
  • Why are you consuming the Exception and not printing any diagnostic information? Print print the stack trace or exception message to Log.e(TAG, ... – JustinDanielson Aug 13 '13 at 20:41
  • I entered your json into a validator and it's invalid. Try verifying that the json is correct. – Collin Flynn Aug 13 '13 at 20:43
  • I can verify that the JSON is correct. I just checked it too.. – Shaunak Aug 13 '13 at 20:44
  • well the thing is...i fetched this json from an internet page... chrome displays it as a xml file and firefox as a json file.....i just checked the log...it says the file is null... – Ankit Srivastava Aug 13 '13 at 20:46
  • also on a side note. What you are doing in the code is not JSON 'parsing' but just reading and writing back JSON. That code should just about work with any string you put in that file and not just JSON . So the JSON tag and title can be misleading got people. I sure was for me. As for some other people here who tried to validate the json :) – Shaunak Aug 13 '13 at 20:55
  • Well yeah....it can be called as the beginning of the parsing ..but that too is not working.. – Ankit Srivastava Aug 13 '13 at 20:58
  • @AnkitSrivastava - Did you see JustinDanielson 's comment above? The code is swallowing the Exceptions, so you will not know if a problem occurs. Like Justin suggested, add some diagnostics. – Leigh Aug 13 '13 at 21:02
  • sorry i forgot to edit....i added the line...and in the console it says that the file is null.. – Ankit Srivastava Aug 13 '13 at 21:04
  • 1
    I don't feel low about myself at all, nor do I feel the need to justify my comment. I do however expect a question to contain at least as much effort as I am prepared to put in to voluntarily helping someone. – Simon Aug 13 '13 at 21:15
  • file is null? Is this line incorrect? new HttpPost("file:///android_asset/www/me.json"); is that path correct? How is me.json included in the project? In the /res/raw folder? – JustinDanielson Aug 13 '13 at 21:27
  • share your exceptions. "file is null" is not verbose enough. try e.getStackTrace() or e.stackTrace() (i forgot the method name) – JustinDanielson Aug 13 '13 at 21:29
  • 1
    @JustinDanielson yes the path is correct ...i have tried using a webivew to display a html file in the same path and it works.. webView.loadUrl("file:///android_asset/www/index.html"); – Ankit Srivastava Aug 14 '13 at 07:48
  • @Simon do you know the correct solution ? – Ankit Srivastava Aug 14 '13 at 07:50
  • @AnkitSrivastava What is the problem? you already have json you need to parse? – Raghunandan Aug 22 '13 at 09:36
  • @Raghunandan this line is the problem... HttpPost httppost = new HttpPost("file:///android_asset/www/me.json"); instead of normal string....when i try to fetch json from a file..it say's...that host may not be null – Ankit Srivastava Aug 22 '13 at 09:38
  • @AnkitSrivastava i don't understand the need for httppost here. it doesn't make sense you want to sent a json file to server? – Raghunandan Aug 22 '13 at 09:40
  • @Raghunandan http://www.technotalkative.com/android-json-parsing/ please have a look at this link....there the guy is using a string...but i want to use a file instead..how can i do that? – Ankit Srivastava Aug 22 '13 at 09:40
  • please have a look at the comment above... – Ankit Srivastava Aug 22 '13 at 09:41
  • @AnkitSrivastava what is that exactly you want. you want to parse a json file which is in assets folder? your problem statement is very unclear. just saying this line causes error does not help. What is it that you want. parsing posting json file to webserver?. I don't get what you are trying to do? To post you need to have a valid url. – Raghunandan Aug 22 '13 at 09:43
  • private String strJSONValue = "{\"FirstObject\":{\"attr1\":\"one value\" ,\"attr2\":\"two value\"," +"\"sub\": { \"sub1\":[ {\"sub1_attr\":\"sub1_attr_value\" },{\"sub1_attr\":\"sub2_attr_value\" }]}}}"; instead of using this string...i want to parse json from internet... – Ankit Srivastava Aug 22 '13 at 09:46
  • @AnkitSrivastava its still not clear what i 1. understand you need to parse a json file that is stored in assests folder?.this can be done. 2. you have a json file in assests folder and you are trying to send the same to server. Which one of the two are you looking for? – Raghunandan Aug 22 '13 at 09:49
  • @Raghunandan http://api.discogs.com/artist/coldplay............i want to parse info from this link – Ankit Srivastava Aug 22 '13 at 09:49
  • it looks like xml not a json to me. a valid json looks like https://gdata.youtube.com/feeds/api/videos?q=random&max-results=50&v=2&alt=jsonc – Raghunandan Aug 22 '13 at 09:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35986/discussion-between-raghunandan-and-ankit-srivastava) – Raghunandan Aug 22 '13 at 09:51
  • @Raghunandan ......yes i want to parse a json file that is stored in assests folder or you may i want to parse a file from internet...which is on this link api.discogs.com/artist/coldplay – Ankit Srivastava Aug 22 '13 at 09:52

2 Answers2

1

create a jsonobject that holds the string then parse each item from this jsonObject like this:

MyResponse =

{"resp": {"status": true, "version": ...

code :

JSONObject json_main = new JSONObject(MyResponse);
JSONObject c = json_main.getJSONObject("resp");
final String status= c.getString("status");

Example

how to work with this: {"resp": {"status": true, "version": "2.0"},"resp":{"status": true, "version": "3.0"}

   JSONObject json_main = new JSONObject(MyResponse);   
    JSONArray main_arr = json_main.getJSONArray("resp");                                 for(int i = 0; i < main_arr.length(); i++)
    {
      JSONObject c = main_arr.getJSONObject(i);
      final String status = c.getString("status ");
    }

enter image description here

Ankit Srivastava
  • 280
  • 2
  • 7
  • 24
Ahmed Alaa El-Din
  • 1,813
  • 1
  • 16
  • 19
0

I think the problem you have here is using the file:// protocol in your HTTP Post. I am not sure if thats valid (but then I am not a android guy). looks like you have that file in a 'www' folder so it must be a web-server of some kind. Why not access it using http:// protocol. Also you don't need post there. You can use HTTP Get just fine.

If you do want to access it using file , use a file reader to read file, something like this:

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}
Shaunak
  • 17,377
  • 5
  • 53
  • 84