InputStream doesn't run-Collection of common programming errors
I’m doing a small android project, which the first feature is to connect to my server and execute a php parser for take some articles. My on create invoke this method:
private String jsonTaker() throws IOException, JSONException {
//call EventsTaker class -> take the 'articles'
JsonReader jsr = new JsonReader();
JSONObject json = jsr.readJsonFromUrl("http://jem88.net/timeSpaceParser.php");
System.out.println(json.toString());
System.out.println(json.get("id"));
return json.toString();
}
this is the readJsonFromUrl()
and readAll()
method:
public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL (url).openStream();
//sia maledetto il creatore di java e tutta la sua genia diojavosogeloso
InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
BufferedReader rd = new BufferedReader(isr);
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
Log.d("readJsonFromUrl", "String to json transformed!");
is.close();
return json;
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
The error appears when on debug mode, the executions arrive to the first line of readJsonFromUrl()
method, where I try to open the stream. Here the application crashes with ‘Source not found’ error. I’ve tried googling about this, but no one of the suggestion given was good for me! So, any suggestion would be great!
EDIT -> I’ve edited the exception’s manage, and now I catch a FATAL Exception. Here is the logcat:
06-04 23:46:56.411: D/AndroidRuntime(22456): Shutting down VM
06-04 23:46:56.411: W/dalvikvm(22456): threadid=1: thread exiting with uncaught exception (group=0x40e312a0)
06-04 23:46:56.436: E/AndroidRuntime(22456): FATAL EXCEPTION: main
06-04 23:46:56.436: E/AndroidRuntime(22456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jem88.timespace/com.jem88.timespace.MainActivity}: android.os.NetworkOnMainThreadException
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.os.Looper.loop(Looper.java:137)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-04 23:46:56.436: E/AndroidRuntime(22456): at dalvik.system.NativeStart.main(Native Method)
06-04 23:46:56.436: E/AndroidRuntime(22456): Caused by: android.os.NetworkOnMainThreadException
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection.(HttpConnection.java:70)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.URL.openStream(URL.java:462)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.jem88.timespace.JsonReader.readJsonFromUrl(JsonReader.java:44)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.jem88.timespace.MainActivity.jsonTaker(MainActivity.java:46)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.jem88.timespace.MainActivity.onCreate(MainActivity.java:23)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.Activity.performCreate(Activity.java:5206)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-04 23:46:56.436: E/AndroidRuntime(22456): ... 11 more