dongyi8416 2016-07-18 08:47
浏览 89

<android studio> HttpClient.execute(httpPost); 即使代码曾经可用,它也被抛入IOException

I'm trying to do a login activity and using these android code and php

==android code for logging in==

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 
url: "+url+"
params: "+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();
    }

}

* once I tested this at school. It's usable (a week ago) I tried again yesterday after I had already updated my phone's OS (oppo F1) and moved to work at house. It's thrown IOException *

The exception says "connection timed out" and there is nothing returned out

I've already changed my IP Address in the code for sure. Using IP get from put "ipconfig" in command prompt.

It's still usable and show correct response when I checked the PHP, so I think It could not be problem with PHP code but I will put it here.

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

?>

The result showed in android app is nothing but it's still show when Itried to run the PHP Script by typing "http://192.168.1.35/_masterpassword_php/getAccountID.php" directly into address bar.

Thanks for helping. Sorry for messed up code. I tried hard to solve it by myself so there could be a lot of comment in code.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大