将android与我的eclipse tomcat服务器连接
Posted
技术标签:
【中文标题】将android与我的eclipse tomcat服务器连接【英文标题】:Connect android with my eclipse tomcat server 【发布时间】:2018-06-06 03:18:39 【问题描述】:我在 eclipse 中运行一个带有 tomcat 服务器的 spring boot 应用程序来接受请求并将它们保存在我的本地数据库中(通过 wamp)。使用 android 作为客户端,我想将登录请求作为从用户输入获得的 json 对象发送,但是当我按下登录按钮时,我的模拟器应用程序崩溃了。 (我还有两个类:UserDAO 和 User(分数为整数的模型类))
这是我的安卓登录活动:
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);
这是我通过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)
【问题讨论】:
你在 logcat 中遇到了什么错误? 哪个语句导致崩溃?请找出答案。 更新了问题。有一个指向 logcat 图像的链接@greenapps 好吧,你是#123456,他有一个 NullPointerException。所以请谷歌。尽管 logcat 会告诉你,但你没有告诉我们是哪个语句导致的。我没有看到 logcat,因为您应该在此处发布 logcat 中的文本。请不要图片。 错误:原因:java.lang.NullPointerException:尝试调用虚拟方法'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request) ' 在空对象引用上@greenapps 【参考方案1】: private RequestQueue queue;
这是一个未初始化的变量。具有空值的指针。因此
queue.add(MyStringRequest);
等于
null.add(MyStringRequest);
这会导致“NullPointerException”。
所以在使用之前初始化你的变量。
【讨论】:
以上是关于将android与我的eclipse tomcat服务器连接的主要内容,如果未能解决你的问题,请参考以下文章
将tomcat jar / lib目录添加到Mac OS X上的eclipse类路径[重复]
使用 Tomcat (Eclipse) 调试 GWT 应用程序