<android studio =“”> HttpClient.execute(httpPost);被抛入IOException甚至代码一旦可用

时间:2016-07-18 08:47:49

标签: php android ioexception connection-timeout

我正在尝试进行登录活动并使用这些Android代码和php

==用于登录的android代码==

public void OnLogin(View v) {

    List<NameValuePair> params_login = new ArrayList<NameValuePair>();
    params_login.add(new BasicNameValuePair("masterUsername", masterUsernameEDT.getText().toString()));
    params_login.add(new BasicNameValuePair("masterPassword", masterPasswordEDT.getText().toString()));

    HTTPConnection hp = new HTTPConnection("192.168.1.35",this); //server ip + current context
    String url_login = "http://"+hp.ip+"/_masterpassword_php/checkLogin.php";

    String result_login  = hp.getHttpPost(url_login,params_login);
---------so on--------

== HTTPconnection ==

    public class HTTPConnection {

    String ip = "localhost";
    Context previousPage ;

    HTTPConnection (String getIP, Context context)
    {   ip = getIP ;
        previousPage = context ;
    }

    public String getHttpPost(String url,List<NameValuePair> params) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        final AlertDialog.Builder a2 = new AlertDialog.Builder(previousPage); //***
        a2.setMessage("In getHttpPost @ HTTPConnection \nurl: "+url+"\nparams: "+params);
        a2.show();

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse response = client.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) { // Status OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            final AlertDialog.Builder a3 = new AlertDialog.Builder(previousPage); //***
            a3.setMessage("IOException : "+e);
            a3.show();
            e.printStackTrace();
        }
        final AlertDialog.Builder a1 = new AlertDialog.Builder(previousPage); //***
        a1.setMessage("json_get : "+str.toString());
        a1.show();
        return str.toString();
    }

}

*一旦我在学校测试了这个。这是可用的(一周前) 我已经更新了手机的操作系统(oppo F1)并搬到了家里工作,我昨天又试了一次。抛出IOException *

例外情况说“连接超时”并且没有返回任何内容

我已经在代码中更改了我的IP地址。使用IP来自命令提示符下的“ipconfig”。

当我检查PHP时,它仍然可以使用并显示正确的响应,所以我认为这不是PHP代码的问题,但我会把它放在这里。

<?php require "connection.php";

//$_POST["masterUsername"] = "test1"; // for Sample
//$_POST["masterPassword"] = "test1"; // for Sample

$masterUsername = $_POST["masterUsername"];
$masterPassword = $_POST["masterPassword"];

$strSQL = "SELECT * FROM masteraccount WHERE 1 
    AND masterUsername = '".$masterUsername."' 
    AND masterPassword = '".$masterPassword."'
    ";
//echo $strSQL;

$objQuery = mysql_query($strSQL) or die(mysql_error());
$objResult = mysql_fetch_array($objQuery);
$intNumRows = mysql_num_rows($objQuery);

if($intNumRows==0) {
    $arr['status'] = "0";
    $arr['accountID'] = "0";
    $arr['error'] = "Incorrect Username and Password"; 
}
else {
    $arr['status'] = "1";
    $arr['accountID'] = $objResult["accountID"];
    $arr['error'] = "-";
}

/**
    $arr['status'] // (0=Failed , 1=Complete)
    $arr['accountID'] // accountID
    $arr['error'] // Error Message
*/
mysql_close($connection);
echo json_encode($arr);

&GT;

在Android应用程序中显示的结果只是在Itried通过直接在地址栏中输入“http://192.168.1.35/_masterpassword_php/getAccountID.php”来运行PHP脚本时仍然显示。

感谢您的帮助。对不起搞砸的代码。我自己努力解决这个问题,所以代码中可能会有很多评论。

0 个答案:

没有答案