[英]http response returns IllegalstateException
我正在創建一個軟件,要求用戶從該軟件登錄。 我已使用Json進行通信。 然后將數據傳輸到我在下面發布的PHP腳本中。 可以一些。 但是它在我的Android代碼中顯示了llegalStateException。 有人可以幫我嗎。
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;
}
}
?>
這是我的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)
看起來您在創建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和JSON時,請考慮使用Retrofit庫。
將http添加到httpPost = new HttpPost("192.168.100.106/EMS/functions.php");
因此它應該是httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");
在您的Php腳本中,您尚未調用自定義函數。 只需添加這兩行就可以了
$func = new functions();
$func->getUser();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.