所以我试图根据两个微调器对象的选择来显示一个特定的结果,所以我在旋转器对象中得到了正确的结果,但我无法正确得到价格的结果。
这是我的微调器对象数据的样子
{
"GetCitiesResult": [
{
"CityID": 1,
"CityName": "----------------"
},
{
"CityID": 2,
"CityName": "التجمع"
},
{
"CityID": 3,
"CityName": "العاشر"
},
{
"CityID": 4,
"CityName": "القرية الزكية"
}
]
}
这就是我的价格数据
{
"GetTripPriceResult": {
"TripID": 0,
"TripName": "",
"TripFrom": "",
"TripTo": "",
"DayPrice": 0,
"MonthPrice": 0,
"TripNotes": "",
"Stations": []
}
}
我希望通过将所选项目的两个字符串结果连接到微调器对象来获取特定旅行的月份价格,这里是获取数据和试图获取价格的代码,我&# 39;我不确定为什么价格不起作用
public class HomeFragment extends Fragment {
private static final String TAG_STRING_URL = "url";
private static final String TAG_CITY_NAME = "CityName";
private static final String TAG_CITY_ID = "CityID";
private static final String TAG_ARRAY_NAME = "GetCitiesResult";
private static String TAG = HomeFragment.class.getSimpleName();
private String tripFromSelected;
private String tripToSelected;
private Spinner spinnerFrom, spinnerTo;
private TextView textView;
private String jsonResponse;
private Context globalContext = null;
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public HomeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
getCities();
}
private void getCities() {
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, TAG_STRING_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ArrayList<City> cities = new ArrayList<City>();
ArrayList<String> cityNames = new ArrayList<String>();
try {
JSONArray jsonArray = response.getJSONArray(TAG_ARRAY_NAME);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jSONObject = jsonArray.getJSONObject(i);
String cityID = jSONObject.getString(TAG_CITY_ID);
String cityName = jSONObject.getString(TAG_CITY_NAME);
City city = new City();
city.setName(jSONObject.optString(TAG_CITY_NAME));
cities.add(city);
cityNames.add(jSONObject.optString(TAG_CITY_NAME));
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
spinnerFrom = (Spinner) getView().findViewById(R.id.tripFromSpinner);
spinnerFrom.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cityNames));
spinnerFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tripFromSelected = spinnerFrom.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerTo = (Spinner) getView().findViewById(R.id.tripToSpinner);
spinnerTo.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, cityNames));
spinnerTo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tripToSelected = spinnerTo.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance(getActivity().getApplicationContext()).addToRequestQueue(jsObjRequest);
}
public void setText(String text){
textView = (TextView) getView().findViewById(R.id.tripPriceText);
textView.setText(jsonResponse);
}
public void getTripPrice() {
String TAG_TRIP_PRICE = "http://url" + tripFromSelected + "/" + tripToSelected;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, TAG_TRIP_PRICE, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try
{
JSONObject getPriceResult = response.getJSONObject("GetTripPriceResult");
String tripPrice = getPriceResult.getString("MonthPrice");
Toast.makeText(globalContext, "Response: " + response, Toast.LENGTH_LONG).show();
}
catch (JSONException e)
{
e.printStackTrace();
Toast.makeText(globalContext, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(globalContext, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance(globalContext).addToRequestQueue(jsonObjReq);
}
这是我的logcat
08-13 23:33:52.455 22127-22127/net.app.cairobus.cairobus W/dalvikvm﹕ VFY: unable to resolve virtual method 434: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-13 23:33:52.455 22127-22127/net.app.cairobus.cairobus W/dalvikvm﹕ VFY: unable to resolve virtual method 456: Landroid/content/res/TypedArray;.getType (I)I
08-13 23:33:52.685 22127-22127/net.app.cairobus.cairobus E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
08-13 23:33:52.685 22127-22127/net.app.cairobus.cairobus E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
08-13 23:33:52.705 22127-22127/net.app.cairobus.cairobus I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:385>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.082_msm8974_refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.082__release_AU ()
OpenGL ES Shader Compiler Version: E031.24.00.06
Build Date: 02/18/14 Tue
Local Branch:
Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_3.5_RB1.04.04.02.006.082
Local Patches: NONE
Reconstruct Branch: NOTHING
08-13 23:33:53.435 22127-22170/net.app.cairobus.cairobus D/libc﹕ [NET] getaddrinfo+,hn 12(0x636169726f6275),sn(),family 0,flags 4
08-13 23:33:53.435 22127-22170/net.app.cairobus.cairobus D/libc﹕ [NET] getaddrinfo-,err=8
08-13 23:33:53.435 22127-22170/net.app.cairobus.cairobus D/libc﹕ [NET] getaddrinfo+,hn 12(0x636169726f6275),sn(),family 0,flags 1024
08-13 23:33:53.435 22127-22170/net.app.cairobus.cairobus D/libc﹕ [NET] getaddrinfo-, 1
08-13 23:33:53.435 22127-22170/net.app.cairobus.cairobus D/libc﹕ [NET] getaddrinfo_proxy+
08-13 23:33:53.435 22127-22170/net.app.cairobus.cairobus D/libc﹕ [NET] getaddrinfo_proxy-, success
找到解决方案:问题是方法getTripPrice()
没有在课堂上调用,所以我做的很简单,我做了一个按钮并在点击方法的按钮内调用它。
答案 0 :(得分:0)
在我看来,问题是AppController.getInstance(globalContext).addToRequestQueue(jsonObjReq);
和globalContext = null;
我无法找到您为globalContext
设置其他值的位置。请检查!
因此,请尝试设置如下值:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
globalContext = container.getContext();
...
}