问题
I'm trying to do a POST request to our backend server, but I keep getting SocketTimeOutExceptions or HTTP 500 error. And it keeps switching between those two randomly (even with the same values).
Things I've already tried
- adding OkHttp library (v2.4.0) for some reason I get the error that it's might not be supported though retrofit says it supports v2.0.0+ source
- implementing
Callback<Account>
into the class instead of this, it also gave me the http500 more than the newer version - this, I've added the UrlFactory and OkHttp libraries and followed these steps but then the entire app crashes (log & changes)
Below is simplyfied code
SignIn
public class SignIn extends Activity implements Callback<Account>{
private static final String TAG = "SignIn";
private ImageButton sign_in_backBtn;
private TextView sign_in_make_account;
private Button btn_SignIn;
private EditText email;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
btn_SignIn = (Button) findViewById(R.id.sign_in_btnSignIn);
email = (EditText)findViewById(R.id.sign_in_email);
password = (EditText)findViewById(R.id.sign_in_password);
initListers();
}
private void initListers() {
btn_SignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String emailAdres = email.getText().toString();
String passWord = password .getText().toString();
if(emailAdresValidate == true && passWordValidate == true){
Account account = new Account(passWord,emailAdres);
login(account);
}
}
});
}
private void login(Account account){
JppApplication.getService().logIn(account,this);
}
@Override
public void success(Account account, Response response) {
Log.d(TAG,response.toString());
Log.d(TAG, "login success");
}
@Override
public void failure(RetrofitError retrofitError) {
Log.e(TAG,retrofitError.getKind().toString());
Log.d(TAG, "login failed");
}
}
Service
public interface Service {
@POST("/accounts/login")
void logIn(@Body Account account,Callback<Account> created);
}
Account
public class Account {
@SerializedName("Email")
private String email;
@SerializedName("Password")
private String paswoord;
public Account(String paswoord, String email) {
this.paswoord = paswoord;
this.email = email;
}
}
possible log/error I get
05-18 09:41:29.800 26126-2080/be.kdgdemo D/Retrofit﹕ java.net.SocketTimeoutException: failed to connect to /10.134.216.25 (port 8017) after 15000ms
at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
05-18 09:41:29.800 26126-2080/be.kdgdemo D/Retrofit﹕ ---- END ERROR
or
05-18 10:04:01.455 6945-7465/be.kdgdemo D/Retrofit﹕ ---> HTTP POST http://10.134.216.25:8017/api/accounts/login
05-18 10:04:01.455 6945-7465/be.kdgdemo D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
05-18 10:04:01.455 6945-7465/be.kdgdemo D/Retrofit﹕ Content-Length: 56
05-18 10:04:01.455 6945-7465/be.kdgdemo D/Retrofit﹕ {"Email":"Gebruikersam@mail.com","Password":"Paswoord1!"}
05-18 10:04:01.455 6945-7465/be.kdgdemo D/Retrofit﹕ ---> END HTTP (56-byte body)
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ <--- HTTP 500 http://10.134.216.25:8017/api/accounts/login (17419ms)
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ : HTTP/1.1 500 Internal Server Error
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Cache-Control: no-cache
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Content-Length: 36
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Content-Type: application/json; charset=utf-8
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Date: Mon, 18 May 2015 08:04:18 GMT
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Expires: -1
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Pragma: no-cache
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ Server: Microsoft-IIS/7.5
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ X-Android-Received-Millis: 1431936258878
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ X-Android-Sent-Millis: 1431936241463
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ X-AspNet-Version: 4.0.30319
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ X-Powered-By: ASP.NET
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ {"Message":"An error has occurred."}
05-18 10:04:18.875 6945-7465/be.kdgdemo D/Retrofit﹕ <--- END HTTP (36-byte body)
回答1:
Ok so turns out the error was because of the following. Our school used a cheap VPN where the server was running on. I always tested this app by running and debugging it on a physical device instead of running it on an emulator.
You had to use the VPN's app to be able to connect to the server, for some reason the connection went down regularly, so whenever we had a timeout exception we just had to restart the VPN connection through the app and we were sure to not receive any timeout exceptions for about a minute or so.
This is what you get when schools try to cut budgets everywhere..
And of course the HTTP-500 was because of an error in our code obviously :)
来源:https://stackoverflow.com/questions/30297996/retrofit-sockettimeoutexception-and-or-http-500-error-on-http-post