我正在使用异步任务来登录用户,虽然php登录页面是我的java android代码:
protected String doInBackground(String... urls) {
String result = "";
for(int i = 0; i <= 0; ++i) {
result = this.invokePost(urls[i], this.postData);
}
return result;
}
private String invokePost(String requestURL, HashMap<String, String> postDataParams) {
String response = "";
try {
URL url = new URL(requestURL);
HttpURLConnection e = (HttpURLConnection)url.openConnection();
/* e.setReadTimeout(15000);
e.setConnectTimeout(15000);*/
e.setRequestMethod("POST");
e.setDoInput(true);
e.setDoOutput(true);
OutputStream os = e.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(this.getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = e.getResponseCode();
String line;
if(responseCode == 200) {
for(BufferedReader br = new BufferedReader(new InputStreamReader(e.getInputStream())); (line = br.readLine()) != null; response = response + line) {
;
}
} else {
response = "";
Log.d("responce code", responseCode + "");
}
} catch (Exception ex) {
response ="An exception has occured" + ex.getMessage();
ex.printStackTrace();
Log.d("exception",ex.toString());
}
return response;
}
Login.java:
HashMap PostData = new HashMap();
PostData.put("username", username);
PostData.put("password", username);
BackgroundWorker LoginWorker= new BackgroundWorker(this,PostData);
LoginWorker.execute("http://192.168.43.22:8080/login.php");
我没有使用模拟器,虽然我通过我的nexus 5手机运行应用程序,我的手机和我的电脑都连接到路由器wifi
但是我得到一个连接异常(我无法通过我的nexus 5 ping我的电脑)
如何在手机和电脑之间打开连接
答案 0 :(得分:0)
使用cmd中的ipconfig
获取您的电脑,并在没有端口的情况下使用它
LoginWorker.execute("http://192.168.**.**/login.php");
如果不在防火墙中打开端口80