2017-08-24 8 views
1

私が使用しているコードから始めましょう、私は "params"を作るためのあらゆる可能な異なる方法を試しました。私はHashMapとして、Json形式で、また文字列としても使用しています。また、ハッシュマップを作成して返すことによってgetParams()メソッドをオーバーライドしようとしました。働いているものはありません。

ここはJsonObjectRequestを呼び出す関数です。

private void sendJsonObjReq() { 

    showProgressDialog(); 
    Map<String, String> para = new HashMap<>(); 

    para.put("Condicao", "dea"); 
    para.put("Field", "1550"); 
    JSONObject jason = new JSONObject(para); 

    String params = jason.toString(); 
    textView.setText(params); 

     JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST, 
       url_cond1, params, 
       new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(AppController.TAG, response.toString()); 
         textView.setText(response.toString()); 

        /*try { 
         int u = response.getInt("sucess"); 
         if(u == 1){ 

          JSONArray json = response.optJSONArray("Type"); 
          if(json != null) { 
           MakeListHashMap(json); 
          }else{ 
           textView.setText("Wrong Parameters"); 
          } 
         }else{textView.setText("success is 0");} 
        }catch(JSONException e){ 
         Log.e("Error:", e.getMessage()); 
         textView.setText("nothing here"); 
        }*/ 
         hideProgressDialog(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(AppController.TAG, "Error:" + error.getMessage()); 
       showProgressDialog(); 
      } 
     }); 
     //Add to request queue 
    AppController.getInstance().addToRequestQueue(jsonReq, tag_json_obj); 

} 

URLおよび他のすべての罰金ですが、私はチェックしているが、私は両方を試してみましたが、私は彼らと任意の成功を持っていなかったので、私はちょうど、GETやPOSTメソッドの仕事なぜどちらを理解することはできません。私のPHPコードは、こので構成されています。私はそれを復号化し、それをデコードしないで試してみました

<?php 
    $response = array(); 
//$oi = json_decode($_POST); 
//$response["Condicao"] = $_GET["Condicao"]; 
//$response["Field"] = $_GET["Field"]; 
    $response["Condicao"] = $_POST["Condicao"]; 
    $response["Field"] = $_POST["Field"]; 

    $response['sucess'] = 1; 
    $response['other'] = "test"; 

    echo json_encode($response); 
?> 

、私は本当に何をすべきかについて途方に暮れています。私はそれがサーバーの問題かもしれないと思うが、アプリケーション "Postman"を使って私はGETとPOSTを送ることができ、応答は私が望むようにJsonの配列です。

私は KEY1 = Condicao =>名前= DEAを送信する場合、 key2 =フィールド=>名前= 1550;

私は結果

{ 
    "Condicao": "dea", 
    "Field": "1550", 
    "sucess": 1, 
    "other": "test" 
} 

編集取得

:それを受信)

<?php 
    $_POST = json_decode(file_get_contents('php://input'), true); 
    $response = array(); 

    $response["Condicao"] = $_POST["Condicao"]; 
    $response["Field"] = $_POST["Field"]; 

    $response['sucess'] = 1; 
    $response['other'] = "test"; 

    echo json_encode($response); 
?> 
+0

$ _ POST配列を使用する場合は、jsonパラメータを送信しないでください。 – greenapps

+0

https://stackoverflow.com/questions/18866571/receive-json-post-with-php –

+0

@Pavneet_Singh私はその質問を読みましたが、私はAndroidアプリを使用しておりますので、私の目的は、データベースを作成するデータを送信することで、私はファイルから読み込むメソッドを使用することはできません。 私の知識が不足しているのは大惨事です。あなたは正しいところとありがたいところです。解決策です。 –

答えて

1

1)の代わりに文字列

JsonObjectRequest jsonReq = new JsonObjectRequest(JsonRequest.Method.POST, 
       url_cond1, jason , 
       //   ^^^^ 
       new Response.Listener<JSONObject>() { 

2.あなたのJSONオブジェクトを渡します。解決策があるが、あなたのPHPで

<?php 
    $response = array(); 
    // receive your json object 
    $jasonarray = json_decode(file_get_contents('php://input'),true); 
    $response["Condicao"] = $jasonarray["Condicao"]; 
    $response["Field"] = $jasonarray["Field"]; 
    $response['sucess'] = 1; 
    $response['other'] = "test"; 

    echo json_encode($response); 
?> 
関連する問題