1

I wrote a wcf REST service (c#) and a client (android). I try to get data from a mysql table via webservice and want to show them in a ListView on an android device. When I debug the sevice I can see, that the data is read and written in a generic List.

But at the end of the method, when the List should return the following error occurs on client site:

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

This is the code for the service interface:

[OperationContract]
[WebGet(UriTemplate = "/{keyword}", ResponseFormat = WebMessageFormat.Json)]
List<object> GetEntity(string keyword);

This is the code for my service method:

public List<object>GetEntity(string keyword)
    {
       conn.Open();
        if (keyword.ToUpper().Equals("USER"))
        {
            List<User> list = new List<User>();


            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            string sql = string.Format("SELECT * FROM " + USERDB);
            command.CommandText = sql;
            reader = command.ExecuteReader();
            String userName = "userName", userPassword = "userPassword", userId = "userId";

            while (reader.Read())
            {
                User user = new User(reader[userId].ToString(), reader[userName].ToString(), reader[userPassword].ToString());
                list.Add(user);
            }
            conn.Close();
            return list;
        }
        else if(keyword.ToUpper().Equals("BOOK"))
        {
             List<Book> list = new List<Book>();
             // some code...
        }
        conn.Close();
        return list;
 }

Stack trace in android eclipse:

09-25 18:14:51.614: W/System.err(10696): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-25 18:14:51.614: W/System.err(10696):    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
09-25 18:14:51.614: W/System.err(10696):    at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
09-25 18:14:51.614: W/System.err(10696):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
09-25 18:14:51.614: W/System.err(10696):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
09-25 18:14:51.614: W/System.err(10696):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
09-25 18:14:51.614: W/System.err(10696):    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-25 18:14:51.624: W/System.err(10696):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-25 18:14:51.624: W/System.err(10696):    at sample.ShowSamples$AsyncSample.doInBackground(ShowSamples.java:105)
09-25 18:14:51.624: W/System.err(10696):    at sample.ShowSamples$AsyncSample.doInBackground(ShowSamples.java:1)
09-25 18:14:51.624: W/System.err(10696):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-25 18:14:51.624: W/System.err(10696):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-25 18:14:51.624: W/System.err(10696):    at java.lang.Thread.run(Thread.java:856)
09-25 18:14:51.624: W/System.err(10696): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.Posix.recvfromBytes(Native Method)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.Posix.recvfrom(Posix.java:131)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
09-25 18:14:51.634: W/System.err(10696):    at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
09-25 18:14:51.634: W/System.err(10696):    ... 24 more

Thank you for any help in advance.

tharos
  • 23
  • 1
  • 5
  • Can you provide more information about the source of the error: is it from Android, from the web service and related to the connection with the mobile device or is it from the web service and related to the MySQL connection? Do you have a stack trace? – Codo Sep 24 '13 at 15:52
  • I added stack trace now. – tharos Sep 26 '13 at 09:01
  • Thanks for adding the stack trace. It shows that the connection has been unexpectedly closed. Most likely, the cause is an error that occurred on the server side. Can you investigate what has happened there? Most likely there was an error in the web service. Have you seen an error there? A stack trace from the server side would be most helpful. – Codo Sep 26 '13 at 09:16
  • I think, the problem on server site is, that I use a generic List with object, but return a generic List with "User" or "Book". How can I write my Service in common, that it handles different kind of objects? – tharos Sep 26 '13 at 10:13
  • Did you ever solve this problem? We have a similar situation. – Rogier van het Schip Nov 14 '13 at 09:26

1 Answers1

1

the problem exists only in android Jelly Bean,

this is my solution

in the connection catch clause,

catch (IOException e) { if (e.getMessage().indexOf("Connection reset by peer") > 0) <<<< Connect gain >>>;

Omar Hassan
  • 727
  • 1
  • 11
  • 24