2016-10-10 19 views
3

其實我跟很多有關我的問題,但沒有回答的是工作,我只是張貼一個字符串,作爲回報,我希望有一個JSON數組POST字符串和應對JSONArray

注意stackoverflow答案:當我運行我硬代碼的腳本這是非常清楚,但在腳本中的POST值顯示空 這裏是我的Android代碼:

private void getData(){ 
    Bundle extras=getIntent().getExtras(); 
    final String id = extras.getString("value").toString().trim(); 
    JSONObject obj =new JSONObject(); 

    final ProgressDialog loading = ProgressDialog.show(Categories.this, "Please wait...","Fetching data...",false,false); 
    Volley.newRequestQueue(this).add(new JsonRequest<JSONArray>(Request.Method.POST, CATEGORIES_URL, obj.toString(), 
        new Response.Listener<JSONArray>() { 
         @Override 
         public void onResponse(JSONArray response) { 
          loading.dismiss(); 
          showList(response); 
         } 
        }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        loading.dismiss(); 
        Toast.makeText(getApplicationContext(), 
          "Ooops!,Internet Connection Problem", Toast.LENGTH_LONG).show(); 

       } 
      }) { 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put(KEY_ID,id); 
        return super.getParams(); 
       } 

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

// RequestQueue requestQueue = Volley.newRequestQueue(this); 
    // requestQueue.add(jsonArrayRequest); 


    Toast.makeText(Categories.this,id,Toast.LENGTH_LONG).show(); 


} 

,並在服務器端我使用$id=$_POST['id']; 但它顯示空 我不千牛流什麼問題

我的PHP腳本:

<?php 
require_once('dbConnect.php'); 
$json = file_get_contents('php://input'); 
$stripe_json= json_decode($json, TRUE); 
$ida=$stripe_json->id; 

$sql= "select title,description,image,price,cid FROM products a where a.cid='".$ida."'"; 
$res=mysqli_query($con,$sql); 
$result = array(); 

while ($row=mysqli_fetch_array($res)){ 
     array_push($result,array('title'=>$row['0'], 
     'description'=>$row['1'], 
     'image'=>$row['2'], 
     'price'=>$row['3'], 

    )); 
    } 
    echo json_encode(($result)); 

    mysqli_close($con); 

?> 
+0

'我只是張貼string'。不,你沒有這樣做。你發佈json。 – greenapps

+0

'而且我想要一個json數組'。你可以想要很多。但是你確定服務器會在json中響應嗎? – greenapps

+0

'當我運行我的腳本時它非常好'。腳本?你的意思是你的java代碼?它很好運行完美。 – greenapps

回答

0

也許你可以使用HTTPPost了點。有可能是因爲cookie的東西,只需要添加此:

CookieManager cookieManager = new CookieManager(); 
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 
    httpPost.setCookieHandler(cookieManager); 

希望它可以幫助!

+0

它不工作是任何方式發送字符串,並在返回得到了JSONArray迴應 – techDigi

+0

即時通訊不知道你是什麼想 - > http://stackoverflow.com/questions/15609306/convert-string-to-json-array但希望這一個幫助。 –

+0

我的動機是將id(字符串)發送到我的服務器,並在我的sql查詢中使用此id並返回json編碼結果(JSONArray) – techDigi

0

這麼多年的努力和指導我的PHP現在工作

代碼:

<?php 
require_once('dbConnect.php'); 
$json = file_get_contents('php://input'); 
$stripe_json= json_decode($json); 
$ida=$stripe_json->id; 

$sql= "select title,description,image,price,cid FROM products a where a.cid='".$ida."'"; 
$res=mysqli_query($con,$sql); 
$result = array(); 

while ($row=mysqli_fetch_array($res)){ 
     array_push($result,array('title'=>$row['0'], 
     'description'=>$row['1'], 
     'image'=>$row['2'], 
     'price'=>$row['3'], 

    )); 
    } 
    echo json_encode(($result)); 

    mysqli_close($con); 

?>