2016-11-18 68 views
1

我有一個問題,與排雷庫得到json。 這是我的JSON:得到具有整數與排球庫json

{"result":1} 

的PHP的MySQL數據庫和顯示結果在這個JSON文件的代碼行數。 計數的數量是整數。

,這是我的代碼得到這個JSON:

private void getData() { 
    //url + id of row that want to count 
     String url = Config.DATA_URL_COUNT +"1"; 

     StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       //loading.setVisibility(View.INVISIBLE); 
       showJSON(response); 
      } 
     }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         // Toast.makeText(this,error.getMessage().toString(), Toast.LENGTH_LONG).show(); 
        } 
       }); 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 

} 

private void showJSON(String response) { 

    int name = 0; 


    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(Config.TAG_RESULT); 
     JSONObject collegeData = result.getJSONObject(0); 
     name = jsonObject.getInt(Config.JSON_ARRAY); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    String count = String.valueOf(name); 
    rowcount_kargozarybime.setText(count); //insert result in textview 
} 

,這是我的PHP代碼:

<?php 

if($_SERVER['REQUEST_METHOD']=='GET'){ 

$categoryid = $_GET['categoryid']; 

    define('HOST','144.76.10.250'); 
    define('USER','cp21983_test'); 
    define('PASS','*******'); 

    define('DB','cp21983_marivanjobs'); 
    $con = mysqli_connect(HOST,USER,PASS,DB); 

    if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
mysqli_query($con,"SET character_set_results=utf8,character_set_client=utf8,character_set_connection=utf8, character_set_database=utf8,character_set_server=utf8"); 

$sql="SELECT * FROM jobs WHERE categoryid='".$categoryid."'"; 

if ($result=mysqli_query($con,$sql)) 
    { 
    // Return the number of rows in result set 
    $rowcount=mysqli_num_rows($result); 
    echo json_encode(array("result"=>$rowcount)); 
    // Free result set 
    mysqli_free_result($result); 
    } 

mysqli_close($con); 

} 

我也應該心存感激,如果有人能幫助我。

+0

而你的問題是什麼? – f1sh

+0

@ f1sh在文本視圖中不顯示結果 –

+0

您可以添加響應結果格式嗎? –

回答

1

嘗試使用:

JSONObject respObject = new JSONObject(response); 
int result = respObject.getInt("result"); 
0

這是一個JSON對象,而不是一個JSON數組。因此使用此代碼得到結果數

JSONObject jsonObject = new JSONObject(response); 
int count = jsonObject.getInt("result"); 
rowcount_kargozarybime.setText(String.valueOf(count)); 
+0

非常感謝你;) –