的java.net.UnknownHostException无法解析主机主机、java、net、UnknownHostException

2023-09-06 13:08:42 作者:死咣咣

我刚一直在试图让一个简单的登录脚本的工作,我的PHP已经过时了蹩脚的,但我有一个iOS的版本相同的应用程序工作正常登录,我知道没有任何事情错了我的PHP脚本,并我需要他们保持不变,或者我想我可以创建一个新的intitled android_login.php 但让工作,我得到了现在。

我的错误是常见的,但我已经尝试了所有常见的修复,其中包括重建VDM设备,甚至现在用我的身体的手机进行测试。我的手机输出的java.net.UnknownHostException无法解析主机 进入我的 et_un 文本编辑继承人我的code

我包括以下到的Andr​​oidManifest.xml

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
 

我的继承人LoginActivity.java 更新时间:

 包com.example.atmebeta;


进口com.loopj.android.http.AsyncHttpClient;
进口com.loopj.android.http.AsyncHtt presponseHandler;
进口com.loopj.android.http.RequestParams;

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

公共类LoginActivity延伸活动{
    联合国的EditText,PW;
    TextView的错误;
    OK按钮;
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.login);
        UN =(EditText上)findViewById(R.id.et_un);
        PW =(EditText上)findViewById(R.id.et_pw);
        OK =(按钮)findViewById(R.id.btn_login);
        误差=(TextView中)findViewById(R.id.tv_error);

        ok.setOnClickListener(新View.OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                AsyncHttpClient客户端=新AsyncHttpClient();
                RequestParams参数=新RequestParams();
                parameters.put(电子邮件,un.getText()的toString());
                parameters.put(密码,pw.getText()的toString());
                client.get(http://www.thatonewebsite.com/login.php,参数,新AsyncHtt presponseHandler(){
                    公众最终无效的onSuccess(字符串响应){
                        error.setText(正确的用户名和密码);
                    }

                    公共无效onFailure(可抛出E,串响应){
                        error.setText(对不起!不正确的用户名或密码);
                    }
                });

            }
        });
    }
}
 

在login.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:后台=#DDDDDD
    工具:上下文=。LoginActivity>

     <的TextView
         机器人:ID =@ + ID / tv_un
         机器人:layout_width =WRAP_CONTENT
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_alignParentTop =真
         机器人:layout_centerHorizo​​ntal =真
         机器人:layout_marginTop =62dp
         机器人:文本=用户名:
         机器人:文字颜色=#444444
         机器人:TEXTSIZE =10PT/>

     <的EditText
         机器人:ID =@ + ID / et_un
         机器人:layout_width =150dip
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_alignLeft =@ + ID / et_pw
         机器人:layout_below =@ + ID / tv_un
         机器人:背景=@机器人:可绘制/ editbox_background
         机器人:EMS =10/>

     <的EditText
         机器人:ID =@ + ID / et_pw
         机器人:layout_width =150dip
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_below =@ + ID / tv_pw
         机器人:layout_centerHorizo​​ntal =真
         机器人:背景=@机器人:可绘制/ editbox_background
         机器人:EMS =10
         机器人:密码=真/>

     <的TextView
         机器人:ID =@ + ID / tv_pw
         机器人:layout_width =WRAP_CONTENT
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_below =@ + ID / et_un
         机器人:layout_centerHorizo​​ntal =真
         机器人:layout_marginTop =14dp
         机器人:文本=密码:
         机器人:文字颜色=#444444
         机器人:TEXTSIZE =10PT/>

     <按钮
         机器人:ID =@ + ID / btn_login
         机器人:layout_width =100dip
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_centerHorizo​​ntal =真
         机器人:layout_centerVertical =真
         机器人:文本=登陆/>

     <的TextView
         机器人:ID =@ + ID / tv_error
         机器人:layout_width =FILL_PARENT
         机器人:layout_height =40dip
         机器人:layout_below =@ + ID / btn_login
         机器人:layout_centerHorizo​​ntal =真
         机器人:layout_marginTop =40dp
         机器人:文字颜色=#AA0000
         机器人:TEXTSIZE =7PT/>

< / RelativeLayout的>
 
体验.NET Core使用IKVM对接Java

和如果需要的login.php

 < PHP

如果(使用isset($ _ GET [电子邮件])及和放大器;使用isset($ _ GET [密码])){
                $电子邮件= $ _GET [电子邮件];
                $密码= $ _GET [密码];
                $结果=登录($电子邮件,$密码);
                回声$结果;
                }

功能makeSqlConnection()
{
$ DB_HOST =localhost的;
$ DB_NAME =bdname;
$ DB_USER =DBUSER;
$ DB_PASS =DBPASS;

    $ CON =的mysql_connect($ DB_HOST,$ DB_USER,$ DB_PASS)或死亡(mysql_error());

        mysql_select_db($ DB_NAME,$ CON)或死亡(mysql_error());

    返回$ CON;
}

功能disconnectSqlConnection($ CON)
{
    则mysql_close($ CON);
}

功能登录($电子邮件,$密码)
{
    //要求(文件);
    $ CON = makeSqlConnection();

    $ SQL =SELECT * FROM用户WHERE电子邮件=$电子邮件和密码='$的密码';
    $水库=的mysql_query($的SQL,$ CON)或死亡(mysql_error());
    $取= mysql_fetch_array($水库);
    $ RES1 = mysql_num_rows($水库);

    disconnectSqlConnection($ CON);

     如果($ RES1!= 0){
        返回$取[电子邮件];
    }其他{
        返回0;
    } //结束其他


}

?>
 

解决方案

这似乎激进,但扔掉你的CustomHttpClient.java并开始使用的库如的 Android的异步HTTP客户端。我会很惊讶,如果你还是切换到一个库,它负责所有的东西给你之后出现了问题。例如:

  AsyncHttpClient客户端=新AsyncHttpClient();
RequestParams RP =新RequestParams();
rp.put(电子邮件,un.getText()的toString());
rp.put(密码,pw.getText()的toString());
client.post(http://www.mywebsite.com/thatoneloginscript.php,RP,新AsyncHtt presponseHandler(){
    @覆盖
    公众最终无效的onSuccess(字符串响应){
        //这里处理您回应
    }

    @覆盖
    公共无效onFailure(可抛出E,串响应){
        // 出了些问题
    }
});
 

Ive just been trying to get a simple login script working, my PHP is out of date its crappy, but i have an iOS version of this same app that works fine logging in, i know nothings wrong with my php scripts, and i need them to stay the same, or i suppose i could create a new one intitled android_login.php but lets work with what i got for now.

My error is common, but i have tried all the common fixes, including recreating VDM device, and even now using my physical phone to test. My phones outputting java.net.unknownhostexception unable to resolve host into my et_un TextEdit heres my code

I am including the following into the AndroidManifest.xml

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

heres my LoginActivity.java UPDATED

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");
                    } 
                });

            }
        });
    }
}

the 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>

and if its needed the 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


}

?>

解决方案

It might seem radical but throw out your CustomHttpClient.java and start using a library such as Android Asynchronous Http Client. I would be very surprised if you still had the problem after switching to a library that takes care of all that stuff for you. Example:

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
    }               
});