所以我有一个webclient正在运行。 webclient创建了JSON文件,我可以使用一个简单的URL在我的android中使用它。 GET工作得很好,但现在我正在接受POST。
我的代码
webclient中的登录方法:
@RequestMapping(value = "/service/login", method = RequestMethod.POST, consumes = "application/json")
public
@ResponseBody
User serviceLogin(@RequestBody User u) {
if (userService.login(u) > -1) {
return userService.findUser(u.getUsername());
} else {
return new User();
}
}
ServerCaller:
public ServerError login(String username, String password) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
try {
String URL = "http://"+ipAddress+"/ProjectTeamF-1.0/service/login.json";
Object[] params = new Object[]{URL,user};
receivedUser = new login().execute(params).get();
} catch (ResourceAccessException rae) {
receivedUser = null;
return ServerError.ServerNotFound;
} catch (HttpServerErrorException hsee) {
receivedUser = null;
return ServerError.WrongData;
} catch(RestClientException rce){
receivedUser = null;
return ServerError.WrongData;
} catch (Exception e) {
System.out.println("error " + e);
receivedUser = null;
return ServerError.OtherError;
}
return ServerError.NoError;
}
查看:
private void instantiateElements() {
login = (Button) findViewById(R.id.login);
main = (Button) findViewById((R.id.main));
serverCaller = ServerCaller.getInstance();
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
serverCaller.login(username.getText().toString(), password.getText().toString());
Toast.makeText(getApplicationContext(),serverCaller.getReceivedUser().getUserID(),Toast.LENGTH_LONG);
}
});
}
异步方法:
public class login extends AsyncTask<Object[],Integer,User> {
@Override
protected User doInBackground(Object[]... params) {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
list.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(list);
User user = new User();
String url = "http://192.168.137.1:8080/ProjectTeamF-1.0/service/login.json";
HttpEntity<User> entity = new HttpEntity<User>(user);
ResponseEntity<User> response = restTemplate.postForEntity(url,entity,User.class);
User u = response.getBody();
return u;
}
}
堆栈跟踪:
03-06 11:33:02.630: ERROR/power(176): *** set_screen_state 1
03-06 11:33:02.670: ERROR/Launcher(723): setWindowOpaque()
03-06 11:33:02.750: ERROR/Launcher(723): MTP-LAUNCHER: media scanning not yet finished.
03-06 11:33:05.130: ERROR/UinboxLogger(3355): [UinboxReceiver] onReceive() >> Context is android.app.ReceiverRestrictedContext@405249f8
03-06 11:33:06.880: ERROR/Launcher(723): setWindowOpaque()
03-06 11:33:09.910: ERROR/Launcher(723): setWindowOpaque()
03-06 11:33:09.930: ERROR/Launcher(723): MTP-LAUNCHER: media scanning not yet finished.
03-06 11:33:10.000: ERROR/Launcher(723): setWindowOpaque()
03-06 11:33:23.540: ERROR/AndroidRuntime(3528): FATAL EXCEPTION: main
java.lang.NullPointerException
at teamf.view.Login$2.onClick(Login.java:61)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
03-06 11:33:23.550: ERROR/(176): Dumpstate > /data/log/dumpstate_app_error
我在视图中从Toast获得NullpointerException。
安迪的想法?提前谢谢!答案 0 :(得分:0)
你应该把你的吐司放在asynctask的onPostExecute中。然后它将在任务完成后触发。现在你立即调用get并且你的请求还没有完成,你的asynktask的doInBackground仍然很忙。