I need load text file from url and write loaded data to String variable. I tried many examples, but I haven any ide for resolve this simple problem. I use min SDK 9 and target SDK version 23. On this code on star app I have info 'Sorry, but Your app is stoped'.
public String GetVersionApk(String addres) {
URL url;
InputStream is = null;
DataInputStream dis;
int line;
StringBuilder x = new StringBuilder();
byte[] bytes = new byte[1000];
try {
url = new URL(addres);
is = url.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((line = dis.read(bytes)) >= 0) {
x.append(new String(bytes, 0, line));
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
}
}
return x.toString();
}
Log cat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.piotr.gm/com.example.piotr.gm.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2370) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2432) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5310) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.InputStream.close()' on a null object reference at com.example.piotr.gm.MapsActivity.GetVersionApk(MapsActivity.java:67) at com.example.piotr.gm.MapsActivity.onCreate(MapsActivity.java:83) at android.app.Activity.performCreate(Activity.java:6865) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2432) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5310) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
EDIT [22.10.2015]: I updated my function, beause I found, that function must run in AsyncTask
private class GetVersionApk extends AsyncTask<String,String,String> {
public AsyncResponse delegate = null;
protected String doInBackground(String... urls) {
URL url;
InputStream is = null;
DataInputStream dis;
int line;
StringBuilder x = new StringBuilder();
byte[] bytes = new byte[1000];
try {
url = new URL(urls[0]);
is = url.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((line = dis.read(bytes)) >= 0) {
x.append(new String(bytes, 0, line));
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try{
is.close();
} catch (IOException ioe) {
}
}
return x.toString();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Log.i("Version APK SERVER", String.valueOf(new GetVersionApk().execute("http://url.example.com/update.php")));
//setUpMapIfNeeded();
}
In console log I revice:
Version APK SERVER﹕ com.example.piotr.gm.MapsActivity$GetVersionApk@24ed514e
On server file return text: 1akkkk
I know, that I must return result in this function, but how?
protected void onPostExecute(String result) {
delegate.processFinish(result);
}