2016-11-01 3 views
0

アンドロイドのVollyを使用してデータベースにリクエストを送信します。これが私の要求VolleyからPHPへのパースリクエスト

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

    @Override 
    protected Double doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     postData(params[0]); 
     return null; 
    } 

    protected void onPostExecute(Double result){ 
     pb.setVisibility(View.GONE); 
     Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_LONG).show(); 
    } 
    protected void onProgressUpdate(Integer... progress){ 
     pb.setProgress(progress[0]); 
    } 

    public void postData(String valueIWantToSend) { 
     try { 
      RequestQueue requestQueue = Volley.newRequestQueue(mContexy); 
      String URL = Config.Get_FeedBackURL; 
      JSONObject jsonBody = new JSONObject(); 
      jsonBody.put("email", SharedPrefs.getString(mContexy, SharedPrefs.KEY_email," ")); 
      jsonBody.put("ID", "ir.gfpishro.mobile"); 
      jsonBody.put("Author", SharedPrefs.getString(mContexy, SharedPrefs.KEY_name," ")); 
      jsonBody.put("comment", valueIWantToSend); 
      final String mRequestBody = jsonBody.toString(); 

      StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Log.i("VOLLEY", response); 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("VOLLEY", error.toString()); 
       } 
      }) { 
       @Override 
       public String getBodyContentType() { 
        return "application/json; charset=utf-8"; 
       } 

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

       @Override 
       protected Response<String> parseNetworkResponse(NetworkResponse response) { 
        String responseString = ""; 
        if (response != null) { 
         responseString = String.valueOf(response.statusCode); 
         responseString = String.valueOf(response.data); 

         // can get more details such as response.headers 
        } 
        return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response)); 
       } 
      }; 

      requestQueue.add(stringRequest); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

これは、PHPのWebサービスに要求を送信しているが、私はそれを扱うことができない、私は私のPHPファイル

$json = file_get_contents('php://input'); 
$objd = json_decode($_POST); 

echo $objd["ID"]; 
//"ID", "ir.gfpishro.mobile" 
    if ($objd["ID"]== "ir.gfpishro.mobile") { 

     echo $obj["email"]; 
     echo $obj["Author"]; 
     echo $obj["comment"]; 

     //fetch product id from ibecon 
     $stmt = $this->db->prepare("INSERT INTO `userComments`(`useremail`, `username`, `text`) VALUES ('as','dd','dddd')"); 

     $stmt->execute(array($obj["email"],$obj["Author"],$obj["comment"])); 

     sendResponse(200, json_encode($obj));   

     return true; 

    }else if(isset($_GET["companyid"])){ 


     sendResponse(203, json_encode($result));   

     return false; 

    } 



} 

PHPのリターンPHP Warning: json_decode() expects parameter 1 to be string, array given inにこのコードを使用して、200のステータスコードを返します。私は知りたいです投稿されたデータをPHPファイルでどのように扱うことができますか? ありがとう

+0

'$ _POST'は、httpサーバ環境内でモジュールとして初期化されるときにPHPが提供するスーパーグローバル配列の1つで、配列_per definition_です。だからあなたはそれをデコードする必要はありません。 – arkascha

+0

@arkaschaありがとう、しかし、私はどのように投稿されたパラメータを取得し、jsonにデコードすることができますか? –

+0

'$ _POST'配列を見てみましょう。それはあなたの質問に答えます。配列をログファイルにダンプするか、デバッガなどを使用します。その後、その配列に含まれる要素に直接アクセスできます。そして、あなたは確かに "jsonにデコード"したくないのです。その '$ _POST'配列内の要素のうちの1つは、jsonでエンコードされた文字列です。次に、_that_文字列をデコードします。しかし、 '$ _POST'配列と' $ _POST ['someKey'] 'のようなキーでアクセスできる配列の要素との間で違いがあります。 – arkascha

答えて

1

これはjson_decodeが文字列をJSONとして受け入れるため、送信しているデータ以外の情報を含む配列であるすべての超大域変数を変換しようとしているからです。 代わりにしてください: $objd = json_decode($json,true);

注意実際のargは、stdObjectではなく配列をキャストすることを意味します。 また、var_dump($ POST)を実行すると、その構造がどのようになっているかがわかります。

+1

ありがとうございました。 –