doudi2833 2015-08-08 15:08
浏览 129
已采纳

http响应返回IllegalstateException

I am creating a software where user is asked to login from the software. I have used Json for the communication. The data is then transmitted to my php script which i have posted below. Can some. But it shows an illegalStateException in my Android code. Can some one pls help me out.

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



        }
    });
}
}

And here is my php script:

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

    }

}



?>

Here is my StackTrace

java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=192.168.100.106/EMS/functions.php
        at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at milind.com.ems.MainActivity$1$1.run(MainActivity.java:79)
  • 写回答

3条回答 默认 最新

  • dow66098 2015-08-08 18:16
    关注

    In your Php script, you have not called your custom functions. Just add these two lines And it should work

     $func = new functions();
     $func->getUser();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)