Reputation: 23
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.
Upvotes: 1
Views: 9629
Reputation: 747
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 >>>;
Upvotes: 1