使用okhttp将Android应用程序与WAMP服务器连接以运行PHP脚本

时间:2017-01-15 16:09:59

标签: php android

我正在尝试使用okhttp依赖关系将Android应用程序与wamp服务器连接以运行简单脚本。但它没有用。 WAMP与Green图标一起正常工作。该计划正在实现onFailure(Call call, IOException e)功能和承诺;

  

注册错误无法在10000ms

之后连接到/192.168.11.100(端口80)

我这样做;

build.gradle中的

okhttpDependency

compile 'com.squareup.okhttp3:okhttp:3.5.0'

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String BASE_URL = "http://192.168.11.100/check.php";
    private OkHttpClient client = new OkHttpClient();
    EditText userNameTextEdit, passwordTextEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //
        userNameTextEdit = (EditText) findViewById(R.id.usenameText);
        passwordTextEdit = (EditText) findViewById(R.id.passwordText);
        Button login = (Button) findViewById(R.id.buttonlogin);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // handle login
                String username = userNameTextEdit.getText().toString();
                String password = passwordTextEdit.getText().toString();
                registerUser(username, password);
            }
        });
    }

    public void registerUser(String username, String password) {
        RequestBody body = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .build();
        Request request = new Request.Builder().url(BASE_URL).post(body).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("Registration Error" + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String resp = response.body().string();
                    Log.v("REGISTER", resp);
                    userNameTextEdit.setText(resp);
                    System.out.println(resp);
                    if (response.isSuccessful()) {
                    } else {

                    }
                } catch (IOException e) {
                    // Log.e(TAG_REGISTER, "Exception caught: ", e);
                    System.out.println("Exception caught" + e.getMessage());
                }
            }
        });
    }
}

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.namal.ntb_server3.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/usenameText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="textPersonName"
        android:text="Name"
        android:id="@+id/passwordText" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_below="@+id/passwordText"
        android:id="@+id/buttonlogin" />
</LinearLayout>

PHP脚本

<?php
    $response= "Incorrect email or password!";
    echo json_encode($response);
?>

关于网络 我正在使用真实设备运行应用程序并使用EVO wingle进行互联网。移动设备使用相同的WIFI USB连接到EVO wingle

修改1 在关闭防火墙时,程序将进入onResponse()并打印;

V/REGISTERActivity: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">                                                         <html><head>                                                                                <title>403 Forbidden</title>                                                                         </head><body>
                                                                                 <h1>Forbidden</h1>
                                                                                 <p>You don't have permission to access /check.php
                                                                                 on this server.<br />
                                                                                 </p>
                                                                                 <hr>
                                                                                 <address>Apache/2.4.18 (Win64) PHP/5.6.19 Server at 192.168.11.100 Port 80</address>
                                                                                 </body></html>

提前致谢。

0 个答案:

没有答案