0

所以我有一个网络客户端正在运行。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();
    }
}

服务器调用者:

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。

安迪的想法?提前致谢!

4

1 回答 1

0

你应该把你的吐司放在你的异步任务的 onPostExecute 中。然后它会在任务完成后触发。现在您立即调用 get 并且您的请求尚未完成,您的 asynctask 的 doInBackground 仍然很忙。

于 2013-03-06T10:06:05.387 回答