I'm facing a strange problem while developing a restlet web service + Android
app.
When I try to recover a String from the web service (deployed locally on Tomcat 7.0) using a simple get, if the string from the server is too big the Android app crashes with a NullPointerException
.
Basically, the scheme is the following
Android app
String retrievedString = clientResource.get();
Web server
return serializedString;
When serializedString
has a (not too) large number of characters (above 2k or something like that), retrievedString
get a null
value.
I don't know what is the problem here, but I think the most likely cause is a memory limit, due to Android heap, Java heap, system RAM or maybe even VirtualBox/Genymotion limitation.
What do you think of the problem and how can I solve it?
As requested, here it is some more code; I tried to avoid unnecessary stuff and try/catch blocks.
Server
ObjectMapper objectMapper = new ObjectMapper();
FilterProvider excludeThreadFilter =
new SimpleFilterProvider().addFilter("filter properties by name",
SimpleBeanPropertyFilter.serializeAllExcept("email", "password",
"threads", "messages", "subforumsSet", /*"parentSubforum",
"registeredDate", "threadSubforum"*/));
String st = objectMapper.writer(excludeThreadFilter).writeValueAsString(lt);
return st;
Client
ClientResource cr = new ClientResource("...an ip address...");
ThreadsInSubforumIdByPage tis = cr.wrap(ThreadsInSubforumIdByPage.class);
// The line below is null
String retrievedString = tis.getThreads();
// The line below raise the NullPointerException
List<Thread> retrievedList =
DataHolder.getHolder().getObjectMapper().readValue(
retrievedString, new TypeReference<List<Thread>>(){});
Exception
EDIT: Ok, I was misled by logcat color. It's only a few days that I'm using Android Studio and I haven't noticed at all that Log.e()
messages are written in normal color.
So, this is what logcat reports
5 .../W/System.err﹕ Unable to convert a [text/plain,UTF-8] representation into an object of class java.lang.String
5 .../W/System.err﹕ [ 175 3749: 3749 W/System.err ]
android.os.NetworkOnMainThreadException
5 .../W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
5 .../W/System.err﹕ at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:158)
5 .../W/System.err﹕ at libcore.io.IoBridge.recvfrom(IoBridge.java:525)
5 .../W/System.err﹕ at java.nio.SocketChannelImpl.readImpl(SocketChannelImpl.java:305)
5 .../W/System.err﹕ at java.nio.SocketChannelImpl.read(SocketChannelImpl.java:267)
5 .../W/System.err﹕ at org.restlet.engine.io.ReadableSocketChannel.read(ReadableSocketChannel.java:82)
5 .../W/System.err﹕ at org.restlet.engine.connector.Connection$1.read(Connection.java:232)
5 .../W/System.err﹕ at org.restlet.engine.io.Buffer.fill(Buffer.java:395)
5 .../W/System.err﹕ at org.restlet.engine.io.ReadableBufferedChannel.onFill(ReadableBufferedChannel.java:157)
5 .../W/System.err﹕ at org.restlet.engine.io.Buffer.process(Buffer.java:601)
5 .../W/System.err﹕ at org.restlet.engine.io.ReadableBufferedChannel.read(ReadableBufferedChannel.java:176)
5 .../W/System.err﹕ at org.restlet.engine.io.Buffer.fill(Buffer.java:395)
5 .../W/System.err﹕ at org.restlet.engine.io.NbChannelInputStream.onFill(NbChannelInputStream.java:189)
5 .../W/System.err﹕ at org.restlet.engine.io.Buffer.process(Buffer.java:601)
5 .../W/System.err﹕ at org.restlet.engine.io.NbChannelInputStream.read(NbChannelInputStream.java:307)
5 .../W/System.err﹕ at java.io.InputStream.read(InputStream.java:163)
5 .../W/System.err﹕ at org.restlet.engine.io.BioUtils.copy(BioUtils.java:81)
5 .../W/System.err﹕ at org.restlet.engine.io.NioUtils.copy(NioUtils.java:148)
5 .../W/System.err﹕ at org.restlet.representation.ReadableRepresentation.write(ReadableRepresentation.java:104)
5 .../W/System.err﹕ at org.restlet.representation.ChannelRepresentation.write(ChannelRepresentation.java:76)
5 .../W/System.err﹕ at org.restlet.representation.ChannelRepresentation.write(ChannelRepresentation.java:82)
5 .../W/System.err﹕ at org.restlet.representation.Representation.getText(Representation.java:397)
9 .../W/System.err﹕ at org.restlet.engine.converter.DefaultConverter.toObject(DefaultConverter.java:260)
9 .../W/System.err﹕ at org.restlet.service.ConverterService.toObject(ConverterService.java:170)
9 .../W/System.err﹕ at org.restlet.resource.Resource.toObject(Resource.java:828)
9 .../W/System.err﹕ at org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:240)
9 .../W/System.err﹕ at $Proxy23.getThreads(Native Method)
9 .../W/System.err﹕ at com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground.loadThreads(SubforumContentActivity.java:329)
9 .../W/System.err﹕ at com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground.access$400(SubforumContentActivity.java:283)
9 .../W/System.err﹕ at com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground$1.run(SubforumContentActivity.java:301)
9 .../W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:615)
9 .../W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
9 .../W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
9 .../W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4745)
9 .../W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
9 .../W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
9 .../W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
9 .../W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
9 .../W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
[ 187 3749: 3749 E/class com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground NullPointerException
java.lang.NullPointerException
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:822)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2153)
at com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground.loadThreads(SubforumContentActivity.java:331)
at com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground.access$400(SubforumContentActivity.java:283)
at com.gmail.tigerjack89.pervasive.forum.SubforumContentActivity$LoadThreadsInBackground$1.run(SubforumContentActivity.java:301)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
In the original question, I omitted that I'm using Jackson
to serialize my entities because seems to be involved only indirectly in this problem. I mean, it raise an NullPointerException
only when retrievedString
is null.
As requested, here is a lot of code from my AsynkTask
private class LoadThreadsInBackground extends LoadFromServerInBackground
{
public LoadThreadsInBackground()
{
super(SubforumContentActivity.this);
}
@Override
protected Void doInBackground(final Boolean... params)
{
runOnUiThread(
new Runnable()
{
@Override
public void run()
{
currentThreadsPosition = threadsListView.getFirstVisiblePosition();
// Appending new data to threadsList
loadThreads(params[0]);
// Setting new scroll position
threadsListView.setSelectionFromTop(currentThreadsPosition + 1, 0);
}
});
return (null);
}
private void loadThreads(boolean reload)
{
if (reload) threadList.clear();
ClientResource cr = new ClientResource("IP address");
cr.setChallengeResponse(getAuthentication());
ThreadsInSubforumIdByPage tis = cr.wrap(ThreadsInSubforumIdByPage.class);
List<Thread> retrievedList;
String s = tis.getThreads();
retrievedList =
getObjectMapper().readValue(
s, new TypeReference<List<Thread>>(){});
threadList.addAll(retrievedList);
if ((retrievedList == null) || (retrievedList.size() < Constants
.RETRIEVED_PER_PAGE))
{
loadMoreThreadsButton.setEnabled(false);
if (retrievedList != null && retrievedList.size() == 0)
{ currentThreadsPage--; }
}
}
}
It extends this abstact class
public abstract class LoadFromServerInBackground extends AsyncTask<Boolean, Void, Void>
{
private ProgressDialog mProgressDialog;
private Activity mInvokerActivity;
public LoadFromServerInBackground(Activity invokerActivity)
{
mInvokerActivity = invokerActivity;
}
@Override
protected void onPreExecute()
{
// Showing progress dialog before sending http request
mProgressDialog = new ProgressDialog(mInvokerActivity);
mProgressDialog.setMessage("Please wait ...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected void onPostExecute(Void unused)
{
// closing progress dialog
mProgressDialog.dismiss();
}
}
You're misusing your AsyncTask
by calling runOnUiThread()
. runOnUiThread()
does exactly what it says it does: executes the code in the Runnable
on the UI thread.
Network communication on Android must be performed on a background thread, runOnUiThread()
breaks that requirement.
Try this version of your AsyncTask
:
private class LoadThreadsInBackground extends LoadFromServerInBackground<List<Thread>> {
int currentThreadsPosition;
boolean reload;
public LoadThreadsInBackground() {
super(SubforumContentActivity.this);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
currentThreadsPosition = threadsListView.getFirstVisiblePosition();
}
@Override
protected List<Thread> doInBackground(final Boolean... params) {
reload = params[0];
// Load data from network
return loadThreads();
}
@Override
protected void onPostExecute(List<Thread> retrievedList) {
super.onPostExecute(retrievedList);
if (reload) {
threadList.clear();
}
// Appending new data to threadsList
threadList.addAll(retrievedList);
if ((retrievedList == null) || (retrievedList.size() < Constants.RETRIEVED_PER_PAGE)) {
loadMoreThreadsButton.setEnabled(false);
if (retrievedList != null && retrievedList.size() == 0) {
currentThreadsPage--;
}
}
threadsListView.setSelectionFromTop(currentThreadsPosition + 1, 0);
}
private List<Thread> loadThreads() {
ClientResource cr = new ClientResource("IP address");
cr.setChallengeResponse(getAuthentication());
ThreadsInSubforumIdByPage tis = cr.wrap(ThreadsInSubforumIdByPage.class);
String s = tis.getThreads();
List<Thread> retrievedList = getObjectMapper().readValue(s, List<Thread>.class);
return retrievedList;
}
}
Then, have it extend this class:
public abstract class LoadFromServerInBackground<T> extends AsyncTask<Boolean, Void, T> {
private ProgressDialog mProgressDialog;
private Activity mInvokerActivity;
public LoadFromServerInBackground(Activity invokerActivity) {
mInvokerActivity = invokerActivity;
}
@Override
protected void onPreExecute() {
// Showing progress dialog before sending http request
mProgressDialog = new ProgressDialog(mInvokerActivity);
mProgressDialog.setMessage("Please wait ...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
@Override
protected void onPostExecute(T unused) {
// closing progress dialog
mProgressDialog.dismiss();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With