【发布时间】:2017-01-01 08:33:15
【问题描述】:
我是 android 新手,我尝试使用 android http post 和 php 进行简单的登录测试,但我无法使用以下代码发送帖子:
package com.yagami.boook.classify;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by allen on 2017/1/1.
*/
public class GetUserInfo extends AsyncTask<String, Integer, String> {
// ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// dialog = ProgressDialog.show(MainActivity.class, "Retrieving User Data", "Please wait...", true);
}
@Override
protected void onPostExecute(String aString) {
super.onPostExecute(aString);
// dialog.dismiss();
}
@Override
protected String doInBackground(String... strings) {
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
Log.d("A","A");
try {
url = new URL("http://localhost/login.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
Log.d("B","B");
request = new OutputStreamWriter(connection.getOutputStream());
Log.d("C","C");
request.write(strings[0]);
request.flush();
request.close();
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String response = reader.readLine();
isr.close();
reader.close();
return response;
} catch (IOException e) {
// Error
}
return null;
}
}
这是 php 文件,我尝试打印一个 txt 文件,以便知道是否收到任何内容:
<?php
$email = $_POST['username'];
$password = $_POST['password'];
// echo $username."<br>";
// echo $password."<br>";
$connect = mysql_connect("localhost" , "root" , "112233");
mysql_select_db("classify", $connect);
$data = mysql_query("select * from users where email = '$email'");
$row = mysql_fetch_row($data);
// echo $row[2];
if($row[2] == $password) echo "pass";
else echo "nopass";
$fp = fopen('xxx.txt', 'w');
fwrite($fp , "abc");
fclose($fp);
?>
这是日志,我在代码中添加了一些 Log.d,以便更容易理解发生了什么。 (如您所见,Log.d("C", "C") 没有出现)
01/01 17:22:50: Launching app
$ adb push C:\Users\allen\Desktop\MyApplication\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.allen.myapplication
$ adb shell pm install -r "/data/local/tmp/com.example.allen.myapplication"
Success
$ adb shell am start -n "com.example.allen.myapplication/com.example.allen.myapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 3148 on device Nexus_5X_API_25 [emulator-5554]
W/System: ClassLoader referenced unknown path: /data/app/com.example.allen.myapplication-2/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.allen.myapplication, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.example.allen.myapplication-2/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 3195: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99477440, error=EGL_BAD_MATCH
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01/01 17:24:54: Launching app
W/System: ClassLoader referenced unknown path: /data/data/com.example.allen.myapplication/lib
E/EGL_emulation: tid 3195: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9947d420, error=EGL_BAD_MATCH
E/EGL_emulation: tid 3195: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x98d37ba0, error=EGL_BAD_MATCH
Hot swapped changes, activity restarted
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
D/A: A
D/B: B
D/A: A
D/B: B
已经搜索了整个谷歌进行调试,但没有发现任何有用的东西,希望这不是一个愚蠢的错误。如果需要更多信息,请通知我。
提前致谢!
【问题讨论】:
-
您是否遇到任何错误?
-
发生这种情况时,您能否捕获 logcat 并在此处发布?
-
添加了日志,里面有一些log.d,希望这样可以清楚
-
您可能无法从您的设备或模拟器访问
http://localhost/。尝试改用 IP 地址。 -
@YuryFedorov 该死的你是认真的-_-