这部分就在这里:JSONObject currentDay = dayArray.getJsONObject(0);
我想要为每个片段更改索引值,但无法绕过它.我尝试过意图并创建一个构造函数但失败了.
就像现在一样,应用程序正在“处理”所有显示星期一时间表的片段(JsONObject索引0).
queryUtils.java
import androID.text.TextUtils;import androID.util.Log;import org.Json.JsONArray;import org.Json.JsONException;import org.Json.JsONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.net.httpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.nio.charset.Charset;import java.util.Array@R_502_6818@;import java.util.@R_502_6818@;public final class queryUtils { private static final String LOG_TAG = queryUtils.class.getSimplename(); //makehttpRequest constants private static final int READ_TIMEOUT = 10000 /* milliseconds */; private static final int CONNECT_TIMEOUT = 15000 /* milliseconds */; private static final int RESPONSE_CODE = 200 /*everything is OK*/; public queryUtils() { } public static @R_502_6818@<Day> fetchDayData(String requestUrl) { URL url = createUrl(requestUrl); String JsonResponse = null; try { JsonResponse = makehttpRequest(url); } catch (IOException e) { Log.e(LOG_TAG,"Problem making the http request.",e); } @R_502_6818@<Day> days = extractFeatureFromJson(JsonResponse); return days; } private static URL createUrl(String stringUrl) { URL url = null; try { url = new URL(stringUrl); } catch (MalformedURLException e) { Log.e(LOG_TAG,"Problem building the URL ",e); } return url; } private static String makehttpRequest(URL url) throws IOException { String JsonResponse = ""; if (url == null) { return JsonResponse; } httpURLConnection urlConnection = null; inputStream inputStream = null; try { urlConnection = (httpURLConnection) url.openConnection(); urlConnection.setReadTimeout(READ_TIMEOUT); urlConnection.setConnectTimeout(CONNECT_TIMEOUT); urlConnection.setRequestMethod("GET"); urlConnection.connect(); if (urlConnection.getResponseCode() == RESPONSE_CODE) { inputStream = urlConnection.getinputStream(); JsonResponse = readFromStream(inputStream); } else { Log.e(LOG_TAG,"Error response code: " + urlConnection.getResponseCode()); } } catch (IOException e) { Log.e(LOG_TAG,"Problem retrIEving Berceni JsON results.",e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (inputStream != null) { inputStream.close(); } } return JsonResponse; } private static String readFromStream(inputStream inputStream) throws IOException { StringBuilder output = new StringBuilder(); if (inputStream != null) { inputStreamReader inputStreamReader = new inputStreamReader(inputStream,Charset.forname("UTF-8")); BufferedReader reader = new BufferedReader(inputStreamReader); String line = reader.readline(); while (line != null) { output.append(line); line = reader.readline(); } } return output.toString(); } private static @R_502_6818@<Day> extractFeatureFromJson(String dayJsON) { if (TextUtils.isEmpty(dayJsON)) { return null; } @R_502_6818@<Day> days = new Array@R_502_6818@<>(); //Try to parse try { JsONObject baseJsonResponse = new JsONObject(dayJsON); JsONArray dayArray = baseJsonResponse.getJsONObject("schedule").getJsONArray("day"); JsONObject currentDay = dayArray.getJsONObject(0); JsONArray getClasses = currentDay.getJsONArray("classes"); for (int j = 0; j < getClasses.length(); j++) { JsONObject currentClass = getClasses.getJsONObject(j); String retrIEveCourseTitle = currentClass.getString("class"); String retrIEveCourseTime = currentClass.getString("time"); String retrIEveCourseTrainer = currentClass.getString("trainer"); String retrIEveCourseCancelState = currentClass.getString("canceled"); Day day = new Day(retrIEveCourseTitle,retrIEveCourseTime,retrIEveCourseTrainer,retrIEveCourseCancelState); days.add(day); } } catch (JsONException e) { // If an error is thrown when executing any of the above statements in the "try" block,// catch the exception here,so the app doesn't crash. Print a log message // with the message from the exception. Log.e("queryUtils","Problem parsing JsON results",e); } return days; }}
还有我的FragmentAdapter.java
import androID.content.Context;import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.app.FragmentPagerAdapter;public class FragmentAdapter extends FragmentPagerAdapter { private Context mContext; public FragmentAdapter(Context context,FragmentManager fm) { super(fm); mContext = context; } @OverrIDe public Fragment getItem(int position) { if (position == 0) { return new MondayFragment(); } else if (position == 1) { return new ThursdayFragment(); } else if (position == 2) { return new WednesdayFragment(); } else if (position == 3) { return new ThursdayFragment(); } else if (position == 4) { return new FrIDayFragment(); } else if (position == 5) { return new SaturdayFragment(); } else { return new SundayFragment(); } } /** * Return the total number of pages. */ @OverrIDe public int getCount() { return 7; } @OverrIDe public CharSequence getPageTitle(int position) { if (position == 0) { return mContext.getString(R.string.monday); } else if (position == 1) { return mContext.getString(R.string.tuesday); } else if (position == 2) { return mContext.getString(R.string.wednesday); } else if (position == 3) { return mContext.getString(R.string.thursday); } else if (position == 4) { return mContext.getString(R.string.frIDay); } else if (position == 5) { return mContext.getString(R.string.saturday); } else { return mContext.getString(R.string.sunday); } }}
JsON示例响应
{ "schedule":{ "day":[ { "ID":"Monday","classes":[ { "class" : "Class","time" : "00:00","trainer" : "Teacher","canceled" : "" },{ "class" : "Class","canceled" : "" } ] },{ "ID":"Tuesday",{ "ID":"Wednesday",{ "ID":"Thursday",{ "ID":"FrIDay",{ "ID":"Saturday",{ "ID":"Sunday","classes":[] } ] }}解决方法 查看您的POJO,这并不明确等于您的JsON.
一天应宣布为:
class Day { String ID; @R_502_6818@<ClassDetail> classes;}class ClassDetail { //all the details}
我认为你错过了接受你的功能,跟随签名不是太明确.
@R_502_6818@<Day> extractFeatureFromJson(String dayJsON)
为了使其更具可读性,我建议将其更改为:
(您可以使用Hashmap as recommended for @ρяσѕρєя):
HashMap<String,ClassDetail> parseScheduleJson(String scheduleJsON)
并将每个ClassDetail添加到结果中
@Nullableprivate static Map<String,@R_502_6818@<ClassDetail>> parseScheduleJson(String scheduleJsON) { if (TextUtils.isEmpty(scheduleJsON)) { return null; } HashMap<String,@R_502_6818@<ClassDetail>> result = new HashMap<>(); try { JsONObject baseJsonResponse = new JsONObject(scheduleJsON); JsONArray dayArray = baseJsonResponse.getJsONObject("schedule").getJsONArray("day"); for (int i = 0; i < dayArray.length(); i++) { Array@R_502_6818@<ClassDetail> classes = new Array@R_502_6818@<>(); JsONObject currentDay = dayArray.getJsONObject(i); for (int j = 0; j < currentDay.getJsONArray("classes").length(); j++) { JsONObject currentClass = currentDay.getJsONArray("classes").getJsONObject(j); String retrIEveCourseTitle = currentClass.getString("class"); String retrIEveCourseTime = currentClass.getString("time"); String retrIEveCourseTrainer = currentClass.getString("trainer"); String retrIEveCourseCancelState = currentClass.getString("canceled"); classes.add(new ClassDetail(retrIEveCourseTitle,retrIEveCourseCancelState)); } result.put(currentDay.getString("ID"),classes); } } catch (JsONException e) { // If an error is thrown when executing any of the above statements in the "try" block,so the app doesn't crash. Print a log message // with the message from the exception. Log.e("queryUtils",e); return null; } return result;}
在此之后,您可以使用以下方式访问当天的课程列表:
mymap.get("Monday");mymap.get("Tuesday");...mymap.get("Sunday");
编辑:
恕我直言,你必须在你的活动中调用你的日常活动,并将地图结果注入你的FragmentPagerAdapter:
public FragmentAdapter(Context context,FragmentManager fm,Map<String,@R_502_6818@<ClassDetail> schedule) { super(fm); mContext = context; mSchedule = schedule;}@OverrIDepublic Fragment getItem(int position) { if (position == 0) { return MondayFragment.newInstance(mSchedule.get("Monday")); //... //same for all conditions //... } else { return SundayFragment.newInstance(mSchedule.get("Sunday")); }}
之后使用newIntance pattern时,您可以将片段声明为:
private @R_502_6818@<ClassDetail> mClasses;public static MondayClassDetailFragment newInstance(Array@R_502_6818@<ClassDetail> classes){ MondayClassDetailFragment myFragment = new MondayClassDetailFragment(); Bundle args = new Bundle(); args.putParcelableArray@R_502_6818@("classes",classes); myFragment.setArguments(args); return myFragment;}@OverrIDepublic voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mClasses = getArguments().getParcelableArray@R_502_6818@("classes");}
PS:在这些日子手动发出http请求和解析Json是浪费精力.我建议你去看看图书馆这样做,尤其是retrofit,它被广泛使用并且有很好的文档记录.
总结以上是内存溢出为你收集整理的android – 在QueryUtils中为每个片段更改值全部内容,希望文章能够帮你解决android – 在QueryUtils中为每个片段更改值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)