2016-12-19 300 views
2

我正在開發簡單的android應用程序,使簡單的發佈請求到服務器。但是它發送兩個作爲請求主體所需的密鑰的空值。我從我的app端檢查它,但它不發送空值。okhttp發送請求發送空值到服務器

這裏是我的php代碼。

<?php 
include 'DBConnection.php'; 

$db = DBConnection::getInstance(); 
$db = DBConnection::getInstance(); 
$mysqli = $db->getConnection(); 

$username = $_POST ['username']; 
$password = $_POST ['password']; 

$result_array = array (
    "username " => $username, 
    "password " => $password 
); 

echo (json_encode($result_array)); 
?> 

這是我的android應用程序中的代碼。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     loginButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new LoginPostRequest().execute(RestConnection.LOGIN_POST); 
      } 
     }); 
    } 

private class LoginPostRequest extends AsyncTask<String, Object, String> { 

     private RequestBody body; 

     @Override 
     protected void onPreExecute() { 
      String userNameData = userName.getText().toString().replace(" ",""); 
      String PasswordData = password.getText().toString().replace(" ",""); 
      JSONObject requestBody = new JSONObject(); 

      try { 
       requestBody.put("username", userNameData); 
       requestBody.put("password", PasswordData); 
      } catch (JSONException e) { 
       Log.d(TAG,"Login onPreExecute"+e.getLocalizedMessage()); 
      } 

      String string = requestBody.toString(); 
      body = RequestBody.create(JSON, string); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String urlEndPoint = params[0]; 

      Request request = new Request.Builder() 
        .url(RestConnection.API_BASE+urlEndPoint) 
        .post(body) 
        .build(); 

      try { 
       Response response = client.newCall(request).execute(); 
       return response.body().string(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return "String"; 

     } 

     @Override 
     protected void onPostExecute(String response) { 
      JSONObject responseBody; 
      try { 
       responseBody = new JSONObject(response); 
       String message = responseBody.getString("message"); 

       if(!message.equals("login failed")){ 
        Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show(); 
        Intent i = new Intent(MainActivity.this,MainMenuActivity.class); 
        startActivity(i); 
        finish(); 
       }else{ 
        Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show(); 
       } 
      } catch (JSONException e) { 
       Log.d(TAG,"Login onPostExecute :"+e.getLocalizedMessage()); 
      } 

     } 
    } 

我使用的是okhttp版本v3.5.0。我究竟做錯了什麼?

+1

會在與REST API交互時推薦使用'Retrofit' /'gson'。 –

+0

看到這個http://stackoverflow.com/questions/32856401/using-okhttp-library-for-posting-to-a-php-script-that-saves-to-mysql它有一個完整的例子.. – rafsanahmad007

+0

@ rafsanahmad007這是okhttp上的舊版本。不是我正在尋找的答案。指出我缺少的代碼中的任何錯誤。 –

回答

0

here找到答案。需要使用FormBody.Builder。由於某些原因,它不像okhttp網站上說的那樣工作。 (我在我的問題中嘗試的方式)。

這是我編輯的AsyncTask類。

private class LoginPostRequest extends AsyncTask<String, Object, String> { 

     private RequestBody body; 
     private HashMap<String,String> postData; 

     @Override 
     protected void onPreExecute() { 
      String userNameData = userName.getText().toString().replace(" ",""); 
      String PasswordData = password.getText().toString().replace(" ",""); 

      postData = new HashMap<>(); 
      postData.put("username",userNameData); 
      postData.put("password",PasswordData); 

      FormBody.Builder builder = new FormBody.Builder(); 

      for (Map.Entry<String, String> entry : postData.entrySet()) { 
       builder.add(entry.getKey(), entry.getValue()); 
      } 

      body = builder.build(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String urlEndPoint = params[0]; 

      Request request = new Request.Builder() 
        .url(RestConnection.API_BASE+urlEndPoint) 
        .post(body) 
        .build(); 
      try { 
       Response response = client.newCall(request).execute(); 
       return response.body().string(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String response) { 

      JSONObject responseBody; 
      try { 
       responseBody = new JSONObject(response); 
       String message = responseBody.getString("message"); 

       if(!message.equals("login failed")){ 
        Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show(); 
        Intent i = new Intent(MainActivity.this,MainMenuActivity.class); 
        startActivity(i); 
        finish(); 
       }else{ 
        Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show(); 
       } 
      } catch (JSONException e) { 
       Log.d(TAG,"Login onPostExecute :"+e.getLocalizedMessage()); 
      } 

     } 
    }