Java 尝试将Emulator上运行的Android应用程序连接到localhost中运行的Servlet
我正在实现一个Android应用程序,它必须与一些Javaservlet通信才能获取数据。 应用程序和servlet之间的连接由httpRequest完成。 java servlet在Tomcat服务器上运行,我将其配置为: JavaServlet代码运行正常,我用桌面版的应用程序对其进行了测试。 问题是当我尝试将应用程序连接到servlet时。 下面是管理与servlet连接的android活动的代码:Java 尝试将Emulator上运行的Android应用程序连接到localhost中运行的Servlet,java,android,servlets,Java,Android,Servlets,我正在实现一个Android应用程序,它必须与一些Javaservlet通信才能获取数据。 应用程序和servlet之间的连接由httpRequest完成。 java servlet在Tomcat服务器上运行,我将其配置为: JavaServlet代码运行正常,我用桌面版的应用程序对其进行了测试。 问题是当我尝试将应用程序连接到servlet时。 下面是管理与servlet连接的android活动的代码: private String user; String DEBUG_TAG
private String user;
String DEBUG_TAG = "debug";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
onStart();
}
public void log(View view) { //action performed when users click on login button
EditText username= findViewById(R.id.username);
EditText password= findViewById(R.id.password);
String account_username=username.getText().toString();
String account_password=password.getText().toString();
String URL = "http://10.0.2.2:8080/Ripetizioni/Login/?uname=" + account_username+ "&psw="+account_password;
if(isOnLine()){
new ExecuteLogin().execute(URL);
}
switch(user) {
case "client":
Intent i = new Intent(this, UserActivity.class);
startActivity(i);
break;
case "administrator":
Intent ii = new Intent(this, AdminActivity.class);
startActivity(ii);
break;
default:
break;
}
}
private class ExecuteLogin extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try {
return getUser(params[0]);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
setUser(s);
}
}
public String getUser(String myurl) {
HttpURLConnection conn = null;
try {
URL url = new URL(myurl);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000); //milliseconds
conn.setConnectTimeout(15000); //milliseconds
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
return readIt(conn.getInputStream());
} catch (Exception ex) {
Log.e("async", ex.getMessage());
ex.printStackTrace();
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private String readIt(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
String line;
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line).append("\n");
}
return result.toString();
}
public boolean isOnLine() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
assert connMgr != null;
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
} else {
assert connMgr != null;
Network[] nets = connMgr.getAllNetworks();
Context context = getApplicationContext();
for (Network net : nets) {
NetworkInfo info = connMgr.getNetworkInfo(net);
assert info != null;
String tipo = info.getTypeName();
boolean connessa = info.isConnected();
if (connessa) {
String tip = "";
if (info.getType() == connMgr.TYPE_MOBILE) {
tip = "Mobile";
} else if (info.getType() == connMgr.TYPE_WIFI) {
tip = "WiFi";
}
Log.d(DEBUG_TAG, "Mobile connected: " + tip);
String text = "internet connessa! " + tipo + " " + tip;
text += info.toString();
Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
toast.show();
return true;
}
}
Toast toast = Toast.makeText(context, "NO RETE", Toast.LENGTH_SHORT);
toast.show();
return false;
}
}
}
如果调试代码,“getUser”方法中会出现如下异常:
java.io.FileNotFoundException: http://10.0.2.2:8080/Ripetizioni/Login/?uname=Mario&psw=012abc
也许我做了一些配置错误或类似的事情,但我没有太多的Android实践,我不知道如何修复这些错误。
谢谢你的帮助
NullPointerException
原因:java.lang.NullPointerException:尝试在null对象引用上调用虚拟方法“int java.lang.String.hashCode()”。我知道这是一个NullPointerException,这是因为getUser
方法捕获异常并重新运行null。我的问题是因为getUser
发现了一个异常。我不明白你想说什么。但是从您的日志:LoginActivity.java:73
第73行的代码导致异常。
java.io.FileNotFoundException: http://10.0.2.2:8080/Ripetizioni/Login/?uname=Mario&psw=012abc