dongyan3018
dongyan3018
2013-04-28 21:15
浏览 641

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
    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
        }               
    });
    
    点赞 评论

相关推荐