【问题标题】:Failing to send POST from android to php无法将 POST 从 android 发送到 php
【发布时间】: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 该死的你是认真的-_-

标签: php android post


【解决方案1】:

您正试图从您的设备或模拟器访问http://localhost/,但您的实际网络服务器在其他地方运行。因此,对于您的设备,此 Web 服务器不是 localhost。

要完成这项工作,您需要确保该设备与您的 Web 服务器连接到同一网络,并且可以从您的设备访问该 Web 服务器(可能存在防火墙问题等)。然后,您需要找出网络内主机的 IP 地址,例如192.168.1.42,并在您的 android 代码中使用该 IP 地址:

// use your valid IP address instead
url = new URL("http://192.168.1.42/login.php");

通过此连接,设备将在您的网络中查找提供的 IP,并访问它(如果可用)。另外,请注意您的 IP 可能会发生变化,因此您需要弄清楚如何将其设为静态。

更多关于从 Android 设备访问 localhost here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2020-01-04
    • 1970-01-01
    • 2014-04-07
    相关资源
    最近更新 更多