Reputation: 4807
I have online JSON file that looks like this:
{
"iPadVersion": "1.0",
"AndroidVersion": "1.0"
}
I am trying to read it like this, (this part of code is in AsyncTask):
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl("http://mylink/db.json");
String str = "tmp";
try {
str = json.getString("AndroidVersion");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG, ""+str);
I get always "tmp" at log.d, it does not work.
StactTrace:
05-16 12:54:08.605: W/System.err(20406): java.io.FileNotFoundException: /http:/mylink/db.json: open failed: ENOENT (No such file or directory)
05-16 12:54:08.605: W/System.err(20406): at libcore.io.IoBridge.open(IoBridge.java:406)
05-16 12:54:08.605: W/System.err(20406): at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-16 12:54:08.605: W/System.err(20406): at java.io.FileInputStream.<init>(FileInputStream.java:105)
05-16 12:54:08.605: W/System.err(20406): at java.io.FileReader.<init>(FileReader.java:66)
05-16 12:54:08.605: W/System.err(20406): at com.weterworks.JSONParser.getJSONFromUrl(JSONParser.java:37)
05-16 12:54:08.605: W/System.err(20406): at com.weterworks.MainActivity$sync.<init>(MainActivity.java:359)
05-16 12:54:08.605: W/System.err(20406): at com.weterworks.MainActivity.onCreate(MainActivity.java:263)
05-16 12:54:08.605: W/System.err(20406): at android.app.Activity.performCreate(Activity.java:4465)
05-16 12:54:08.605: W/System.err(20406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-16 12:54:08.605: W/System.err(20406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-16 12:54:08.605: W/System.err(20406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-16 12:54:08.605: W/System.err(20406): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-16 12:54:08.605: W/System.err(20406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-16 12:54:08.605: W/System.err(20406): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 12:54:08.605: W/System.err(20406): at android.os.Looper.loop(Looper.java:137)
05-16 12:54:08.605: W/System.err(20406): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-16 12:54:08.605: W/System.err(20406): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:54:08.615: W/System.err(20406): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 12:54:08.615: W/System.err(20406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-16 12:54:08.615: W/System.err(20406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-16 12:54:08.615: W/System.err(20406): at dalvik.system.NativeStart.main(Native Method)
05-16 12:54:08.615: W/System.err(20406): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
05-16 12:54:08.615: W/System.err(20406): at libcore.io.Posix.open(Native Method)
05-16 12:54:08.615: W/System.err(20406): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
05-16 12:54:08.615: W/System.err(20406): at libcore.io.IoBridge.open(IoBridge.java:390)
05-16 12:54:08.615: W/System.err(20406): ... 20 more
05-16 12:54:08.615: W/System.err(20406): org.json.JSONException: No value for AndroidVersion
05-16 12:54:08.615: W/System.err(20406): at org.json.JSONObject.get(JSONObject.java:354)
05-16 12:54:08.615: W/System.err(20406): at org.json.JSONObject.getString(JSONObject.java:510)
05-16 12:54:08.615: W/System.err(20406): at com.weterworks.MainActivity$sync.doInBackground(MainActivity.java:404)
05-16 12:54:08.615: W/System.err(20406): at com.weterworks.MainActivity$sync.doInBackground(MainActivity.java:1)
05-16 12:54:08.615: W/System.err(20406): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-16 12:54:08.615: W/System.err(20406): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-16 12:54:08.625: W/System.err(20406): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-16 12:54:08.625: W/System.err(20406): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-16 12:54:08.625: W/System.err(20406): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-16 12:54:08.625: W/System.err(20406): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-16 12:54:08.625: W/System.err(20406): at java.lang.Thread.run(Thread.java:856)
JsonParser Class:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
try {
BufferedReader reader = new BufferedReader(new FileReader(url));
String line, results = "";
while ((line = reader.readLine()) != null) {
results += line;
}
reader.close();
jObj = new JSONObject(results);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jObj;
}
}
Upvotes: 0
Views: 1932
Reputation: 80603
You are trying to read data from a URL, but you are using a FileReader
in your stream chain. This simply won't work, as a FileReader
interprets its string argument as a file on your local system (see Javadoc).
What you need to do is use a HTTP library to download the data from the given URL, then parse it into a JSON object. I show an example below, using the built in HTTP classes in the SDK:
public class JSONParser {
private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31";
public JSONObject getJSONFromUrl(String url) {
try {
final HttpURLConnection connection = (HttpURLConnection) new URL(
url).openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
if (connection.getResponseCode() == 200) {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line = null;
final StringBuffer buffer = new StringBuffer(4096);
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
final JSONObject jObj = new JSONObject(buffer.toString());
return jObj;
} else {
return null;
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
Side note: Instead of creating your own parsing utility for this, you could include Jackson in your project, and use this one-liner:
final JsonNode jsonObj = new ObjectMapper().readTree(new URL(
"http://www.example.com/json"));
Upvotes: 3
Reputation: 1478
Check your server link and use it properly. I think, You have the problem with the server URL. log your url string and try to get it through browser.
Upvotes: 0