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

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题