第一个请求正常执行,但相同的第二个请求返回错误400。 从浏览器,此请求执行完整,通常多次。 这很奇怪。也许,我不懂AsyncTask。 这是请求类:
private class LoginTask extends AsyncTask<Void,Void,String>{
@Override
protected String doInBackground(Void... voids) {
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
lgn = (EditText)findViewById(R.id.login) ;
pass = (EditText)findViewById(R.id.password) ;
return template.getForObject(Constants.URL.HOST+"login?login="+lgn.getText()+"&pass="+pass.getText(), String.class);
}
@Override
protected void onPostExecute(String str) {
String result = (str.substring(1,str.length()-1)).trim();
TextView rView = (TextView)findViewById(R.id.resView);
rView.setText(result);
if (result.equals("user")){
global gl = (global) LoginActivity.this.getApplication();
gl.setLogin(lgn.getText().toString());
Intent intent = new Intent(LoginActivity.this, UsersActionActivity.class);
startActivity(intent);
}
}
}
从android studio记录错误:
01-10 09:54:19.052 8119-8140 / com.team.sarafanka.sarafanka W / RestTemplate:GET请求&#34; http://192.168.1.47:8080/login/login=lox&password=1234&#34;结果是400();调用错误处理程序 01-10 09:54:19.062 8119-8140 / com.team.sarafanka.sarafanka E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1 流程:com.team.sarafanka.sarafanka,PID:8119 java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:353) 在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) 在java.util.concurrent.FutureTask.run(FutureTask.java:271) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636) 在java.lang.Thread.run(Thread.java:764) 引起:org.springframework.web.client.HttpClientErrorException:400 at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88) 在org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:585) 在org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:541) 在org.springframework.web.client.RestTemplate.execute(RestTemplate.java:499) 在org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264) 在com.team.sarafanka.sarafanka.activities.LoginActivity $ LoginTask.doInBackground(LoginActivity.java:45) 在com.team.sarafanka.sarafanka.activities.LoginActivity $ LoginTask.doInBackground(LoginActivity.java:36) 在android.os.AsyncTask $ 2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636) 在java.lang.Thread.run(Thread.java:764)
服务器代码:
@RestController
public class LoginController {
@Autowired
private AccountRepository accRep;
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseBody
public String loginQuery(@RequestParam (value ="login",required = true,defaultValue = "") String lgn,@RequestParam (value ="pass",required = true,defaultValue = "") String pass){
String targetLogin = lgn.trim();
String targetPass = pass.trim();
String result = "none";
List<Account> list = accRep.findAll();
for (Account acc:list) {
if (acc.getLogin().equals(targetLogin) && acc.getPassword().equals(targetPass)){
result = acc.getAccountType();
}
}
return result;
}}