我正在尝试向后端服务器发出POST请求,但我一直收到SocketTimeOutExceptions或HTTP 500错误。 并且它随机地在这两个之间切换(即使具有相同的值)。
我已经尝试过的事情
Callback<Account>
实施到班级而不是this,它还给了我比新版本更多的http500 以下是简单的代码
登入
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");
}
}
服务
public interface Service {
@POST("/accounts/login")
void logIn(@Body Account account,Callback<Account> created);
}
帐户
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;
}
}
可能的日志/错误
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
或
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)
答案 0 :(得分:0)
好的,所以证明错误是由于以下原因。我们学校使用了运行服务器的廉价VPN。我总是通过在物理设备上运行和调试它来测试这个应用程序,而不是在模拟器上运行它。
您必须使用VPN的应用程序才能连接到服务器,由于某种原因连接经常停止,因此每当我们遇到超时异常时,我们只需通过应用程序重新启动VPN连接我们肯定不会在大约一分钟左右收到任何超时异常。
这是学校在各处削减预算时所得到的。
当然,HTTP-500是因为我们的代码中出现了错误:)