我正在創建一個軟件,要求用戶從軟件登錄。我已經使用Json進行溝通。然後將數據傳輸到我在下面發佈的php腳本中。可以一些。但它在我的Android代碼中顯示了一個illegalStateException。有人可以幫我解決問題嗎?http響應返回IllegalstateException
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;
}
}
?>
這裏是我的堆棧跟蹤
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)
這是超級IMPORTAN包括完整堆棧跟蹤,這樣我們就可以看到,並提到在'IllegalStateException'發生 – pelotasplus
完成。我更新了我的代碼。 –
謝謝,一個答案正在等着你;-) – pelotasplus