我想对JSON解析的网址进行硬编码。下面是我的代码:
我将SERVER_API_URL
更改为TEST_API_URL
,但它不起作用。所以,我如何设置硬编码网址。
我的JSON解析编码与其他在线JSON解析不同。请帮我看看。感谢。
private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
//Before running code in the separate thread
@Override
protected void onPreExecute() {
//Create a new progress dialog
progressDialog = ProgressDialog.show(getActivity(), "Loading...",
"Loading exam timetable, please wait...", false, false);
}
//The code to be executed in a background thread.
@Override
protected Void doInBackground(Void... params) {
Context ctx = getActivity();
SharedPreferences prefs = ctx.getSharedPreferences(Constants.PREF_NAME, 0);
try {
// res1 = HttpClient.getData(ctx, Constants.SERVER_API_URL + "/timetable/semester/" + prefs.getString("Username", ""));
res1 = HttpClient.getCode(ctx, Constants.TEST_API_URL + "/timetable/semester/" + prefs.getString("Username", "")); //Testing using with my own Url but not working
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//after executing the code in the thread
@Override
protected void onPostExecute(Void result) {
//close the progress dialog
progressDialog.dismiss();
//initialize the View
if ((res1 != null) && (res1.getHttpCode() == 200)) {
ViewPager pager = (ViewPager) timetable.findViewById(R.id.pager_day);
String res = res1.getBody();
week = new Gson().fromJson(res, WeeklyTimetable.class);
if ((week.getMondayActivities().size() == 0) && (week.getTuesdayActivities().size() == 0) &&
(week.getWednesdayActivities().size() == 0) && (week.getFridayActivities().size() == 0) &&
(week.getThursdayActivities().size() == 0) && (week.getSaturdayActivities().size() == 0) &&
(week.getSundayActivities().size() == 0)) {
RelativeLayout rl = (RelativeLayout) timetable.findViewById(R.id.semesterDay);
rl.removeAllViews();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rl.addView(View.inflate(timetable.getContext(), R.layout.fragment_layout_timetable_fail, null), params);
} else {
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
pager.setAdapter(buildAdapter(res));
if (day == 1) {
int finalDay = 6;
pager.setCurrentItem(finalDay);
} else {
int finalDay = day - 2;
pager.setCurrentItem(finalDay);
}
pager.setOffscreenPageLimit(7);
}
} else {
RelativeLayout rl = (RelativeLayout) timetable.findViewById(R.id.semesterDay);
rl.removeAllViews();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rl.addView(View.inflate(timetable.getContext(), R.layout.fragment_layout_fail, null), params);
TextView text = (TextView) timetable.findViewById(R.id.unavailable);
String str = res1.getBody();
int index = str.indexOf("\"}");
text.setText(str.substring(12, index));
}
}
}
下面是 Constants.SERVER_API_URL 和 Constants.TEST_API_URL
的类public class Constants {
/* Hosting Setting */
/* Production */
public static final String SERVER_HOST = "Androidmobile.app.com"; //example
public static final String SERVER_API_URL = "https://Androidmobile.app.com/mobile/api";
public static final String SERVER_API_URL_HTTPS = "https://Androidmobile.app.com/mobile/api";
//Testing Dummy Data
public static final String TEST_API_URL = "http://kyawmyohtet.my3gb.com/ExamTimetable.php";
//End Testing
}
答案 0 :(得分:0)
在Asynctask
班级
String url = null;
LoadViewTask (String url){
this.url = url;
}
在您的onCreate
方法中或您创建AsyncTask
实例的任何地方,
LoadViewTask task1 = new LoadViewTask("Your URL");
然后,您可以在url
课程的任何位置使用变量AsyncTask
来访问您的网址。
答案 1 :(得分:0)
中的代码更改
private class LoadViewTask extends AsyncTask<Void, Integer, Void> {
到
private class LoadViewTask extends AsyncTask<String, Integer, Void> {
和 doInBackground()方法
protected Void doInBackground(String... params)
{
Context ctx = getActivity();
try {
res1 = HttpClient.getCode(ctx, params[0]);
//Testing using with my own Url but not working
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
并使用硬编码 url 调用 LoadViewTask ,如下所示。
SharedPreferences prefs = this.getSharedPreferences(Constants.PREF_NAME, 0);
String str_URL = Constants.TEST_API_URL + "/timetable/semester/" + prefs.getString("Username", "");
new LoadViewTask().execute(str_URL);
这是您在 LoadViewTask 类中传递的硬编码网址,无论您何时何地致电回复。