dongyan3018 2013-04-28 21:15
浏览 1005
已采纳

java.net.unknownhostexception无法解析主机

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


}

?>
  • 写回答

1条回答 默认 最新

  • douchanxiu5636 2013-04-28 21:34
    关注

    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
        }               
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3