2016-03-24 32 views
0

我已覆蓋getParams(),mEmail,mUsername等全局聲明。Android Volley getParams()方法沒有被調用JsonObjectRequest

@Override 
public Map<String, String> getParams() throws AuthFailureError { 
    Map<String,String> params = new Hashtable<String, String>(); 

    params.put("Email", mEmail.getText().toString().trim()); 
    params.put("Username",mUsername.getText().toString().trim()); 
    params.put("Password",mPassword.getText().toString().trim()); 
    params.put("BirthDay",mBirthday.getText().toString().replaceAll("\\s","-")); 
    params.put("Sex",SelectedRadio()); 
    params.put("Bitmap",getEncodedBitmap()); 

    return params; 
} 

這是我的服務器端代碼:

<?php 
require "init1.php"; 

$email = $_POST["Email"]; 
$password = $_POST["Password"]; 
$Username = $_POST["Username"]; 
$Sex = $_POST["Sex"]; 
$ProfilePicture = $_POST["Bitmap"]; 

$timestamp = strtotime($_POST["BirthDay"]); 
$BirthDay = date('Y-m-d',$timestamp); 

$sql = "select * from Registered_Users where Username='$Username'"; 
$sqle = "select * from Registered_Users where Email='$email'"; 
try{ 
    $result1 = mysqli_query($con, $sqle) or die(mysqli_error($con));; 
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));; 
} catch(Exception $e) { 
    echo $e->getMessage(); 
} 

if (mysqli_num_rows($result) > 0){ 
    $myclass = new stdClass(); 
    $myclass->status="Not Ok"; 

    echo json_encode($myclass); 
} else if(mysqli_num_rows($result1) > 0) { 
    $myclass = new stdClass(); 
    $myclass->status="Not Ok"; 

    echo json_encode($myclass); 
} else{ 
    try{ 
     $sql_query = "insert into Registered_Users (Email, Password, BirthDay, Sex, Username, ProfilePicture) values('$email', '$password', '$BirthDay', '$Sex', '$Username', '$ProfilePicture')"; 

     if(mysqli_query($con,$sql_query)){ 
      $obj1 = new stdClass(); 
      $obj1->status="Ok"; 

      echo json_encode($obj1); 
     } else { 
      $obj1 = new stdClass(); 
      $obj1->status="Not Ok "; 
     } 
    } catch(Exception $e) { 
     echo $e->getMessage(); 
    } 
} 
?> 

我得到的迴應總是not ok,具有因我已經有一個空行,在我的數據庫,我試圖再次上傳一個空行。有人可以告訴我爲什麼無法獲取數據嗎?爲什麼我會收到空​​的數據?

+0

你能夠在服務器上接收這些參數嗎? –

+0

我認爲不行!這就是爲什麼它是空的!因爲我嘗試用敬酒來檢查mEmail.getText()中的內容,它向我展示了價值。你知道這個解決方案嗎? – Jois

回答

2

使用這個定製的凌空請求類

import com.android.volley.NetworkResponse; 
import com.android.volley.ParseError; 
import com.android.volley.Request; 
import com.android.volley.Response; 
import com.android.volley.Response.ErrorListener; 
import com.android.volley.Response.Listener; 
import com.android.volley.toolbox.HttpHeaderParser; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.UnsupportedEncodingException; 
import java.util.Map; 

public class CustomJsonRequest extends Request<JSONObject> { 

    private Listener<JSONObject> listener; 
    private Map<String, String> params; 

    public CustomJsonRequest(String url, Map<String, String> params, 
          Listener<JSONObject> reponseListener, ErrorListener errorListener) { 
     super(Method.GET, url, errorListener); 
     this.listener = reponseListener; 
     this.params = params; 
    } 

    public CustomJsonRequest(int method, String url, Map<String, String> params, 
          Listener<JSONObject> reponseListener, ErrorListener errorListener) { 
     super(method, url, errorListener); 
     this.listener = reponseListener; 
     this.params = params; 
    } 

    @Override 
    protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { 
     return params; 
    } 

    ; 

    @Override 
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String jsonString = new String(response.data, 
        HttpHeaderParser.parseCharset(response.headers)); 
      return Response.success(new JSONObject(jsonString), 
        HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      return Response.error(new ParseError(e)); 
     } catch (JSONException je) { 
      return Response.error(new ParseError(je)); 
     } 
    } 

    @Override 
    protected void deliverResponse(JSONObject response) { 
     // TODO Auto-generated method stub 
     listener.onResponse(response); 
    } 
} 

你可以用

Map<String,String> params = new Hashtable<String, String>(); 

params.put("Email", mEmail.getText().toString().trim()); 
params.put("Username",mUsername.getText().toString().trim()); 
params.put("Password",mPassword.getText().toString().trim()); 
params.put("BirthDay",mBirthday.getText().toString().replaceAll("\\s","-")); 
params.put("Sex",SelectedRadio()); 
params.put("Bitmap",getEncodedBitmap()); 



CustomJsonRequest request = new CustomJsonRequest(Request.Method.POST, url, params, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 

      } 
     }, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 

    } 
}); 

request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
AppController.getInstance().getRequestQueue().add(request); 

其實這個問題與凌空發送參數使用它。檢查這個答案 https://stackoverflow.com/a/27091088/1320616

編輯:解釋這種行爲

您JsonObjectRequest類從JsonRequest從申請延長延伸。

您的請求類中有一個方法

/** 
    * Returns the raw POST or PUT body to be sent. 
    * 
    * <p>By default, the body consists of the request parameters in 
    * application/x-www-form-urlencoded format. When overriding this method, consider overriding 
    * {@link #getBodyContentType()} as well to match the new body format. 
    * 
    * @throws AuthFailureError in the event of auth failure 
    */ 
    public byte[] getBody() throws AuthFailureError { 
     Map<String, String> params = getParams(); 
     if (params != null && params.size() > 0) { 
      return encodeParameters(params, getParamsEncoding()); 
     } 
     return null; 
    } 

通知,這種方法被調用getParams()方法的方法。在撥打電話時,您重寫的方法也是一樣的。

但如果你看看JsonRequest類裏面,有一個方法

@Override 
    public byte[] getBody() { 
     try { 
      return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET); 
     } catch (UnsupportedEncodingException uee) { 
      VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", 
        mRequestBody, PROTOCOL_CHARSET); 
      return null; 
     } 
    } 

這意味着請求的getBody()由JsonRequest類的getBody()已被重寫,這意味着你的getParams()方法將永遠不會得到調用。所以你需要一個直接擴展Request類的自定義類,並且不會覆蓋Request類的getBody()方法。

+0

但這是如何有所作爲?我寫的和你寫的東西幾乎是一樣的,因爲我在這裏的一切都是你要在overrdien方法中實現的東西 – Jois

+0

有一個小小的差異。您的普通JsonObjectRequest類繼承自JsonRequest ,而您的CustomJsonRequest從請求擴展。我確信原因,但在過去我也面臨同樣的問題,這解決了我的問題。將做一些研究並回來。 –

+0

更新瞭解釋。現在檢查。 –

相關問題