2015-08-08 94 views
2

我正在創建一個軟件,要求用戶從軟件登錄。我已經使用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) 
+0

這是超級IMPORTAN包括完整堆棧跟蹤,這樣我們就可以看到,並提到在'IllegalStateException'發生 – pelotasplus

+0

完成。我更新了我的代碼。 –

+0

謝謝,一個答案正在等着你;-) – pelotasplus

回答

0

在你的PHP腳本,你有沒有叫你的自定義功能。 只需添加這兩條線,它應該工作

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

它看起來像你忘了創建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庫。

+0

確定工作。但仍然我的意圖不會加載 –

+0

,因此請確保'response'確實是'1',正如您期望的那樣;-)或者檢查logcat以獲取更多錯誤,例如缺少活動或其他內容。 – pelotasplus

+0

我的android設備沒有得到任何迴應,這就是它不工作的原因 –

1

添加HTTP來 httpPost = new HttpPost("192.168.100.106/EMS/functions.php");

所以應該 httpPost = new HttpPost("http://192.168.100.106/EMS/functions.php");

+1

可以讓你自己清楚一點嗎? –

+0

@Milind編輯了包含多一點的答案 –

+0

我的意圖仍未加載!我應該做出什麼樣的必要改變 –