Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php http响应返回IllegalstateException_Php_Android_Json_Http - Fatal编程技术网

Php http响应返回IllegalstateException

Php http响应返回IllegalstateException,php,android,json,http,Php,Android,Json,Http,我正在创建一个软件,要求用户从该软件登录。我使用Json进行通信。然后将数据传输到我下面发布的php脚本。我可以买一些。但它在我的Android代码中显示了一个非法状态异常。谁能帮我一下吗 public class MainActivity extends AppCompatActivity { EditText userName; EditText password; Button sButton; HttpClient httpClient; HttpPost httpPost; HttpR

我正在创建一个软件,要求用户从该软件登录。我使用Json进行通信。然后将数据传输到我下面发布的php脚本。我可以买一些。但它在我的Android代码中显示了一个非法状态异常。谁能帮我一下吗

public class MainActivity extends AppCompatActivity
{
EditText userName;
EditText password;
Button sButton;
HttpClient httpClient;
HttpPost httpPost;
HttpResponse httpResponse;
String username;
String pass;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userName = (EditText)findViewById(R.id.user_id);
    password = (EditText) findViewById(R.id.user_password);
    sButton= (Button) findViewById(R.id.s_button);
    username = userName.getText().toString();
    pass = password.getText().toString();
    httpClient = new DefaultHttpClient();

    httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
    final JSONObject jsonObject = new JSONObject();


    sButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
                Thread thread = new Thread()
                {
                    @Override
                    public void run()
                    {
                        try

                        {
                            jsonObject.put("username", username);
                            jsonObject.put("password", pass);
                            Log.wtf("Sent data :","username and password");
                            httpPost.setEntity(new StringEntity(jsonObject.toString()));
                        }

                        catch(JSONException | UnsupportedEncodingException e)

                        {
                            e.printStackTrace();
                        }
                        try {
                            httpResponse = httpClient.execute(httpPost);
                            String str = EntityUtils.toString(httpResponse.getEntity());
                            JSONObject responseObject = new JSONObject(str);
                            String response = responseObject.getString("success");
                            if(response.equals("1"))
                            {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getApplicationContext(), "Credentials match successful.",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(getApplicationContext(),index.class);
                                        startActivity(intent);

                                    }
                                });
                            }

                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };thread.start();



        }
    });
}
}
下面是我的php脚本:

<?php

class functions  
{
function __construct() 
{
    require_once 'DB_Connect.php';
    $this->db = new DB_Connect();
    $this->db->connect();
}
function __destruct() 
{

}

function getUser()
{
    $json= file_get_contents("php://input");
    $str = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($json));
    $str = json_decode($str,true);
    $ID = $str['username'];
    $user = mysqli_query($com, 'SELECT * FROM employers WHERE Employers_ID = {$ID}');
    $db_password = $user['Password'];
    if($user->num_rows >0)
    {
        //$json= file_get_contents("php://input");
        //$str = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($json));            
        $password = $str['password'];
        $password = clearstring($password);
        compare($db_password,$password);
    }
    function compare($db_str, $app_str)
    {   
       // $salt = "ol2ujh354238095uwef";
        $app_str = $app_str/*.$salt*/;
        if(md5($app_str)==$db_str)
        {
            $response['success'] = '1';
        }
        else
        {
            $response['success'] = '0';
        }
        return json_encode($response);
        //$response = json_encode($response);
        die(json_encode($response));
        //mysqli_close($con);
    }            
    }
    function clearstring($str)
    {
        //$str = strip_tags($str);
        $str = stripcslashes($str);
        $str = htmlspecialchars($str);
        $str = trim($str);
    return $str;

    }

}



?>

在创建
HttpPost
对象时,您似乎忘记了提及协议

你有:

httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
应该是这样

httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");
查看缺少的
http://
。当然,如果使用SSL,请使用https://

此外,考虑在使用后端、API和JSONs时使用库。

< P>添加HTTP到
httpPost=newhttppost(“192.168.100.106/EMS/functions.php”)

应该如此


httpPost=新的httpPost(“http://192.168.100.106/EMS/functions.php");

在Php脚本中,您没有调用自定义函数。 只要加上这两行就行了

 $func = new functions();
 $func->getUser();

包含完整的stacktrace非常重要,这样我们就可以看到何时何地提到
IllegalStateException
happeneddone。我已经更新了我的代码。谢谢,答案正在等待您;-)因此,请确保
响应
确实是
1
,正如您预期的那样;-)或者检查logcat是否有更多错误,比如缺少活动或其他什么。我的android设备没有收到任何响应,这就是它不工作的原因。我的意图仍然没有加载!我应该做什么必要的改变
 $func = new functions();
 $func->getUser();