我正在尝试将数据插入WAMP服务器数据库。我使用System.out.print
语句打印了一系列序列,以了解代码失败的位置。从下面提到的代码中,执行后的代码:“步骤5”失败。
我还在模拟器的浏览器中测试了URL。它显示错误为
net :: ERR CONNECTION_TIMED_OUT
似乎连接无法建立到WAMP服务器。
以下是代码片段:
protected String doInBackground(String... params) {
String reg_url = "https://10.0.2.2/webapp/registration.php";
String login_url = "https://10.0.2.2/webapp/login.php";
System.out.println("Step 1");
String method = params[0];
if(method.equals("register")) {
String uname = params[1];
String pass = params[2];
String full_name = params[3];
try {
System.out.println("Step 2");
URL url = new URL(reg_url);
System.out.println("Step 3");
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
System.out.println("Step 4");
httpsURLConnection.setRequestMethod("POST");
System.out.println("Step 5");
httpsURLConnection.setDoOutput(true);
OutputStream os = httpsURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
System.out.println("Step 6");
String data = URLEncoder.encode("uname","UTF-8") +"="+ URLEncoder.encode(uname,"UTF-8") +"&"+ URLEncoder.encode("pass","UTF-8") +"="+ URLEncoder.encode(pass,"UTF-8") +"&"+ URLEncoder.encode("full_name","UTF-8") +"="+ URLEncoder.encode(full_name, "UTF-8");
bufferedWriter.write(data);
System.out.println("Data writen");
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream is = httpsURLConnection.getInputStream();
is.close();
return "Registration Succesful";
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("URL issue");
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO Error");
}
}
return null;
}`
以下是关于logcat的声明:
12-17 16:44:57.582 18840-18902/com.vinitmandlesha.logintestform I/System.out: Step 5
12-17 16:45:09.947 18840-18847/com.vinitmandlesha.logintestform W/art: Suspending all threads took: 28.687ms
12-17 16:46:01.004 18840-18902/com.vinitmandlesha.logintestform W/System.err: java.net.ConnectException: failed to connect to /10.0.2.2 (port 443): connect failed: ETIMEDOUT (Connection timed out)
12-17 16:46:01.038 18840-18902/com.vinitmandlesha.logintestform W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
12-17 16:46:01.054 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
12-17 16:46:01.054 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
12-17 16:46:01.055 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.net.Socket.connect(Socket.java:882)
12-17 16:46:01.055 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)
12-17 16:46:01.055 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.Connection.connect(Connection.java:148)
12-17 16:46:01.055 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
12-17 16:46:01.055 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
12-17 16:46:01.056 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
12-17 16:46:01.056 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
12-17 16:46:01.056 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208)
12-17 16:46:01.057 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
12-17 16:46:01.057 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
12-17 16:46:01.058 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.vinitmandlesha.logintestform.BackgroundTask.doInBackground(BackgroundTask.java:51)
12-17 16:46:01.058 18840-18902/com.vinitmandlesha.logintestform W/System.err: at com.vinitmandlesha.logintestform.BackgroundTask.doInBackground(BackgroundTask.java:21)
12-17 16:46:01.058 18840-18902/com.vinitmandlesha.logintestform W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-17 16:46:01.058 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-17 16:46:01.096 18840-18902/com.vinitmandlesha.logintestform W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-17 16:46:01.097 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-17 16:46:01.098 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-17 16:46:01.098 18840-18902/com.vinitmandlesha.logintestform W/System.err: at java.lang.Thread.run(Thread.java:818)
12-17 16:46:01.122 18840-18902/com.vinitmandlesha.logintestform W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
12-17 16:46:01.127 18840-18902/com.vinitmandlesha.logintestform W/System.err: at libcore.io.Posix.connect(Native Method)
12-17 16:46:01.128 18840-18902/com.vinitmandlesha.logintestform W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
12-17 16:46:01.128 18840-18902/com.vinitmandlesha.logintestform W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
12-17 16:46:01.160 18840-18902/com.vinitmandlesha.logintestform W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)
12-17 16:46:01.161 18840-18902/com.vinitmandlesha.logintestform W/System.err: ... 20 more
12-17 16:46:01.164 18840-18902/com.vinitmandlesha.logintestform I/System.out: IO Error
12-17 16:46:01.565 18840-18847/com.vinitmandlesha.logintestform W/art: Suspending all threads took: 25.339ms
解决方案:?