我有一个错误,没有为add(String)方法找到合适的方法Collection.add(Hostel)不适用(参数不匹配; String无法转换为Hostel).how将字符串转换为宿舍..我已经发送了我的完整代码。然后字符串值被转换为Hostel对象
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest( Request.Method.GET, url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject data = response.getJSONObject("response");
JSONArray jsonArray =data.getJSONArray( "products" );
for(int i=0; i<jsonArray.length(); i++)
{
JSONObject jsonObject=jsonArray.getJSONObject( i );
jsonLatitude = jsonObject.getString("latitude");
jsonLongitude = jsonObject.getString("longitude");
double distance=distance( Double.parseDouble( jsonLatitude ),Double.parseDouble(jsonLongitude ),GeoLat,GeoLong);
if(!(3 <= distance)){
jsonName = jsonObject.getString("pname");
//price means phone number 1
jsonMobile1 = jsonObject.getString("price");
//discount means phone number 2
jsonMobile2=jsonObject.getString( "discount" );
// address means pdescription details
jsonAddress=jsonObject.getString("pdescription");
//area means city name
jsonArea=jsonObject.getString( "area" );
JSONArray jsonImage = jsonObject.getJSONArray("image");
jsonPhoto = jsonImage.getString(0);
hostelList.add( jsonName );
hostelList.add(jsonMobile1);
}
}
shimmerFrameLayout.stopShimmerAnimation();
shimmerFrameLayout.setVisibility( View.GONE);
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);
答案 0 :(得分:0)
实际上,对于这个问题,您必须创建一个包含字符串和其他变量的自定义模型类。这是根据您对json所需数据的需求。
然后ArrayList<ModelClass> nameArrray=new ArrayList<>();