2017-02-12 90 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文件。 –

相关问题