【问题标题】:java.net.unknownhostexception unable to resolve hostjava.net.unknownhostexception 无法解析主机
【发布时间】:2013-04-28 21:15:27
【问题描述】:

我一直试图让一个简单的登录脚本正常工作,我的 PHP 已经过时了,它很糟糕,但是我有一个 iOS 版本的同一个应用程序可以正常登录,我知道我的 php 脚本没有任何问题,并且我需要它们保持不变,或者我想我可以创建一个名为 android_login.php 的新名称,但让我们使用我现在得到的东西。

我的错误很常见,但我已经尝试了所有常见的修复方法,包括重新创建 VDM 设备,甚至现在使用我的物理手机进行测试。我的手机将java.net.unknownhostexception unable to resolve host 输出到我的et_un TextEdit 这是我的代码

我将以下内容添加到AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

这是我的 LoginActivity.java 更新

package com.example.atmebeta;


import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
    EditText un,pw;
    TextView error;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        un=(EditText)findViewById(R.id.et_un);
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AsyncHttpClient client = new AsyncHttpClient();
                RequestParams parameters = new RequestParams();
                parameters.put("email", un.getText().toString());
                parameters.put("password", pw.getText().toString());
                client.get("http://www.thatonewebsite.com/login.php", parameters, new AsyncHttpResponseHandler() {
                    public final void onSuccess(String response) {
                        error.setText("Correct Username or Password");
                    }

                    public void onFailure(Throwable e, String response) {
                        error.setText("Sorry!! Incorrect Username or Password");
                    } 
                });

            }
        });
    }
}

login.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DDDDDD"
    tools:context=".LoginActivity">

     <TextView
         android:id="@+id/tv_un"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="62dp"
         android:text="User Name:"
         android:textColor="#444444"
         android:textSize="10pt" />

     <EditText
         android:id="@+id/et_un"
         android:layout_width="150dip"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/et_pw"
         android:layout_below="@+id/tv_un"
         android:background="@android:drawable/editbox_background"
         android:ems="10" />

     <EditText
         android:id="@+id/et_pw"
         android:layout_width="150dip"
         android:layout_height="wrap_content"
         android:layout_below="@+id/tv_pw"
         android:layout_centerHorizontal="true"
         android:background="@android:drawable/editbox_background"
         android:ems="10"
         android:password="true" />

     <TextView
         android:id="@+id/tv_pw"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/et_un"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="14dp"
         android:text="Password:"
         android:textColor="#444444"
         android:textSize="10pt" />

     <Button
         android:id="@+id/btn_login"
         android:layout_width="100dip"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:text="Login" />

     <TextView
         android:id="@+id/tv_error"
         android:layout_width="fill_parent"
         android:layout_height="40dip"
         android:layout_below="@+id/btn_login"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="40dp"
         android:textColor="#AA0000"
         android:textSize="7pt" />

</RelativeLayout>

如果它需要 login.php

<?php

if (isset($_GET["email"])  && isset($_GET["password"]) ){
                $email = $_GET["email"];
                $password = $_GET["password"];
                $result = login( $email, $password);
                echo $result;
                }

function makeSqlConnection()
{
$DB_Host = "localhost";
$DB_Name = "bdname";
$DB_User = "dbuser"; 
$DB_Pass = "dbpass";

    $con = mysql_connect($DB_Host,$DB_User,$DB_Pass) or die(mysql_error()); 

        mysql_select_db($DB_Name,$con) or die(mysql_error()); 

    return $con;
}

function disconnectSqlConnection($con)
{
    mysql_close($con);
}

function login($eMail, $password)
{
    //require (FILE);
    $con = makeSqlConnection();

    $sql = "SELECT * FROM user WHERE email = '$eMail' AND password = '$password'";
    $res = mysql_query($sql,$con) or die(mysql_error());
    $fetch = mysql_fetch_array($res);
    $res1 = mysql_num_rows($res);

    disconnectSqlConnection($con);

     if ($res1 != 0) {
        return $fetch['email'];
    }else{
        return 0;
    }// end else


}

?>

【问题讨论】:

    标签: php android httpclient


    【解决方案1】:

    这可能看起来很激进,但扔掉您的 CustomHttpClient.java 并开始使用诸如Android Asynchronous Http Client 之类的库。如果您在切换到为您处理所有这些内容的库后仍然遇到问题,我会感到非常惊讶。示例:

    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.put("email", un.getText().toString());
    rp.put("password", pw.getText().toString());
    client.post("http://www.mywebsite.com/thatoneloginscript.php", rp, new AsyncHttpResponseHandler() {
        @Override
        public final void onSuccess(String response) {
            // handle your response here
        }
    
        @Override
        public void onFailure(Throwable e, String response) {
            // something went wrong
        }               
    });
    

    【讨论】:

    • 感谢您的建议,我会试一试
    • 我正在尝试寻找没有 JSON 的解决方案
    • Android Async Http Client 可以根据需要使用 JSON,但不是必须的。我将用一个简单的例子进行更新。
    • 非常感谢您根本不知道这一点。
    • 由于某种原因,当我使用您的代码时,它不再给我同样的错误,但它说这是它的登录,即使我清楚地输入了错误的电子邮件和密码。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2013-09-07
    相关资源
    最近更新 更多