我需要从服务器下载JSON文件。 一旦下载,我需要运行第二个URL并从那里下载文件。 所有这些都需要在用户看到任何内容之前发生(除了加载对话框)。
我想要的是从url下载JSON并获取姓氏(lname),并获取名字(fname)。 然后我需要构建一个像:http://google.nl/?user=lname&user2=fname的网址。 然后我需要运行该URL,从中获取所有详细信息并在我的listView中显示它(我知道如何做到这一点)。 唯一的问题是我如何实现这一目标? 我的想法是在onPostExecute中调用一个新方法来调用另一个执行AsyncTask的类,但这没有用...
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
JSONObject first = contacts.getJSONObject(1);
String lastname = first.getString(TAG_LASTNAME);
String firstname = first.getString(TAG_FIRSTNAME);
if(lastname != null){
url1 = "http://project-hr.nl/serviceview.php?&email="+lastname+"&pass="+firstname;
ServiceHandler sh1 = new ServiceHandler();
String jsonStrShare = sh1.makeServiceCall(url1, ServiceHandler.GET);
try {
JSONObject jsonObj1 = new JSONObject(jsonStrShare);
contacts1 = jsonObj1.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_FIRSTNAME);
String lastsharename = c.getString(TAG_LASTNAME);
String email = c.getString(TAG_EMAIL);
String list = c.getString(TAG_LIST);
String tussenv = c.getString(TAG_TUSSENV);
String pass = c.getString(TAG_PASS);
String phone = c.getString(TAG_PHONENUMBER);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_FIRSTNAME, id);
contact.put(TAG_LASTNAME, lastsharename);
contact.put(TAG_EMAIL, email);
contact.put(TAG_LIST, list);
contact.put(TAG_TUSSENV, tussenv);
contact.put(TAG_PASS, pass);
contact.put(TAG_PHONENUMBER, phone);
//contact.put(TAG_PHONE_MOBILE, mobile);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
}
答案 0 :(得分:0)
对于那些有同样问题的人。这是用于下载JSON并使用第一个JSON参数再次下载JSON的代码。
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (!jsonStr.isEmpty()) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
JSONObject first = contacts.getJSONObject(0);
Log.d("response lastname ", first.getString("lname"));
Log.d("response firstname ", first.getString("fname"));
String lastname = first.getString("lname");
String firstname = first.getString("fname");
Log.i("check if empty", ""+lastname);
if(!lastname.isEmpty()){
url1 = "SPECIFY URL";//Put URL there
ServiceHandler sh1 = new ServiceHandler();
String jsonStrShare = sh1.makeServiceCall(url1, ServiceHandler.GET);
JSONObject jsonObj1 = new JSONObject(jsonStrShare);
contacts1 = jsonObj1.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts1.length(); i++) {
JSONObject c = contacts1.getJSONObject(i);
String id = c.getString(TAG_FIRSTNAME);
String lastsharename = c.getString(TAG_LASTNAME);
String email = c.getString(TAG_EMAIL);
String list = c.getString(TAG_LIST);
String tussenv = c.getString(TAG_TUSSENV);
String pass = c.getString(TAG_PASS);
String phone = c.getString(TAG_PHONENUMBER);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_FIRSTNAME, id);
contact.put(TAG_LASTNAME, lastsharename);
contact.put(TAG_EMAIL, email);
contact.put(TAG_LIST, list);
contact.put(TAG_TUSSENV, tussenv);
contact.put(TAG_PASS, pass);
contact.put(TAG_PHONENUMBER, phone);
//contact.put(TAG_PHONE_MOBILE, mobile);
// adding contact to contact list
contactList.add(contact);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}