[英]http response returns IllegalstateException
I am creating a software where user is asked to login from the software. 我正在创建一个软件,要求用户从该软件登录。 I have used Json for the communication. 我已使用Json进行通信。 The data is then transmitted to my php script which i have posted below. 然后将数据传输到我在下面发布的PHP脚本中。 Can some. 可以一些。 But it shows an illegalStateException in my Android code. 但是它在我的Android代码中显示了llegalStateException。 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脚本:
<?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 这是我的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)
It looks like your forgot to mention protocol when creating HttpPost
object. 看起来您在创建HttpPost
对象时忘记提及协议。
You have: 你有:
httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
and it should be 它应该是
httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");
See that missing http://
. 看到缺少的http://
。 Of course, use https://
if you're using SSL. 当然,如果您使用的是SSL,请使用https://
。
Moreover, consider using Retrofit library when playing with backend, APIs and JSONs. 此外,在使用后端,API和JSON时,请考虑使用Retrofit库。
Add http to httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
将http添加到httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
So it should be httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");
因此它应该是httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");
In your Php script, you have not called your custom functions. 在您的Php脚本中,您尚未调用自定义函数。 Just add these two lines And it should work 只需添加这两行就可以了
$func = new functions();
$func->getUser();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.