2016-03-24 8 views
0

getParams()をオーバーライドし、mEmailmUsernameなどがグローバルに宣言されています。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(); 
    } 
} 
?> 

私は取得しています応答は、私はすでに私のデータベースに空の行を持っている、と私は再び空の行をアップロードしようとしていますbecasuse、常に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

EDIT:この動作の説明

あなたJsonObjectRequestクラスはリクエストから延びJsonRequestから延びています。あなたのリクエストクラスの内部

この方法は、()メソッドのgetParamsを呼び出していること方法

/** 
    * 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; 
    } 

通知があります。通話中にオーバーライドするのと同じ方法です。あなたはJsonRequestクラス内見れば

はしかし、あなたのgetParams()を取得することはありません意味法

@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; 
     } 
    } 

それはJsonRequestクラスのgetBody()によって上書きされたリクエストのgetBody()を意味していますと呼ばれる。そのため、Requestクラスを直接拡張し、RequestクラスのgetBody()メソッドをオーバーライドしないカスタムクラスが必要です。

+0

しかし、これはどのように違いがありますか?私が書いたものとあなたが書いたものはほぼ同じです。なぜなら、ここで重要なことは、あなたがoverrdienメソッドの体で実装しようとしているものだからです。 – Jois

+0

小さな違いがあります。通常のJsonObjectRequestクラスはJsonRequest から継承し、CustomJsonRequestはRequest から継承します。私はその理由については確信していますが、過去にも私は同じ問題に直面し、これが私の問題を解決しました。いくつかの研究をして戻ってくるだろう。 –

+0

は説明を更新しました。今すぐチェックしてください。 –

関連する問題