gpt4 book ai didi

java - 将 android 与我的 eclipse tomcat 服务器连接

转载 作者:行者123 更新时间:2023-11-28 23:58:46 24 4
gpt4 key购买 nike

我在 eclipse 中运行一个带有 tomcat 服务器的 spring boot 应用程序来接受请求并将它们保存在我的本地数据库中(通过 wamp)。使用 android 作为客户端,我想将登录请求作为从用户输入中获取的 json 对象发送,但是当我按下登录按钮时,我的模拟器应用程序崩溃了。(我还有两个类:UserDAO 和 User(分数为整数的模型类))

这是我的安卓登录 Activity :

package com.example.mujtaba.quizzer;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import com.example.mujtaba.quizzer.Activity.QuizMaking;
import com.example.mujtaba.quizzer.Activity.QuizTaking;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.Map;

public class Login extends AppCompatActivity {
private Button button;
private TextView username;
private TextView password;
private Spinner role;
private String url = "http://localhost:8080/users/signup";
private RequestQueue queue;
private ProgressDialog progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

username=(TextView) findViewById(R.id.username);
password=(TextView) findViewById(R.id.password);
button=(Button) findViewById(R.id.button);
role = (Spinner) findViewById(R.id.role);

// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.role_spinner, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
role.setAdapter(adapter);
}


public void Quiz(View v) { //select a new activity on the basis of role




StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//This code is executed if the server responds, whether or not the response contains data.
//The String 'response' contains the server's response.
}
}, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyError error) {
//This code is executed if there is an error.
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String,String>();
MyData.put("Username", username.getText().toString() ); //Add the data you'd like to send to the server.
MyData.put("Password",password.getText().toString());
MyData.put("Role",role.getSelectedItem().toString());
MyData.put("Score","0");
return MyData;
}
};

queue.add(MyStringRequest);
}
}

这是我的 Controller ,用于通过 eclipse 处理 tomcat 服务器中的请求:

package com.nust.QuizApplication.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nust.QuizApplication.dao.UserDAO;
import com.nust.QuizApplication.model.User;

@Controller
@RequestMapping(value = "/users", produces = "application/json")
public class UserController {
@Autowired
private UserDAO userDao;


@RequestMapping(value = "/signup" ,method = RequestMethod.POST, consumes = "application/json" )
@ResponseBody
public int create(@RequestBody User user) {
int id = userDao.signupUser(user);
if(id>0)
return id;
else
return 0;
}


@RequestMapping(value = "/signin",method = RequestMethod.POST, consumes = "application/json" )
@ResponseBody
public boolean signinUser(@RequestBody User user) {
boolean check = userDao.signinUser(user);
if(check==true)
return true;
else
return false;
}

}

Logcat(This is incompletely shown in the image:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)' on a null object reference)

最佳答案

 private RequestQueue queue;

这是一个未初始化的变量。具有空值的指针。因此

queue.add(MyStringRequest);

等于

null.add(MyStringRequest);

这会导致“NullPointerException”。

所以在使用之前初始化你的变量。

关于java - 将 android 与我的 eclipse tomcat 服务器连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47958963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com