Skip to content Skip to sidebar Skip to footer

Json Parsing In Android Is Showing No Results,illegal Argument Exception,host Name May Not Be Null

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 erro

Solution 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 = newJSONObject(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 = newJSONObject(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

Solution 2:

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 fileStringBuildertext=newStringBuilder();

try {
    BufferedReaderbr=newBufferedReader(newFileReader(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
}

Post a Comment for "Json Parsing In Android Is Showing No Results,illegal Argument Exception,host Name May Not Be Null"