2017-02-12 93 views
0

我試圖將我的應用程序連接到服務器,但出現錯誤。請幫忙!錯誤:(54,91)錯誤:不兼容的類型:int不能轉換爲字符串

我試圖用凌空庫到服務器的連接我的應用程序,但每當我嘗試運行我的代碼,我得到以下錯誤

incompatible types: int cannot be converted to String

我試圖從字符串改變爲int,但它沒沒有幫助!

show.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,   **<-------------error here** 
          showUrl, new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          try { 
           JSONArray student = response.getJSONArray("student"); 
           for (int i=0;i<student.length();i++){ 
            JSONObject students = student.getJSONObject(i); 

            String firstname = students.getString("firstname"); 
            String lastname = students.getString("lastname"); 
            String age = students.getString("age"); 

            result.append(firstname+""+lastname+""+age+""+"\n"); 
           } 
           result.append("==\n"); 

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

         } 
        }, new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 

         } 
        }); 
        requestQueue.add(jsonObjectRequest); 
       } 
      }); 
     } 
    } 
+0

你的轉換在哪裏? int是那個年齡? –

+0

我試過這樣做 int age = students.getInt(「age」); 但它沒有幫助! –

+1

@AnujGajbhiye你可以分享你的'JSON'迴應嗎? –

回答

1

現在就遇到您的問題。

您使用的不正確的方法JsonObjectRequestVolley

你調用這個方法

JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)

,而不是

JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)

,其中第一個參數是一個int

你傳遞4個參數,所以被稱爲上的方法在您int method被轉換爲String url

你可以通過5個參數修復它,一切應該是好的呢。

+0

我試着做這個'int age = students.getInt(「age」);'但它沒有幫助! –

+0

@AnujGajbhiye請分享您的'JSON'迴應。它可能會清除混淆 –

+0

這是我的應用程序的所有代碼..rest是初始化和xml文件。 –

相關問題