我正在開發大學在線結果檢查android應用程序,以便學生在文本框中輸入他/她的註冊號碼結果應該顯示。我有大學的網址,但我不知道如何提取json格式的網址,並將其稱爲android請任何人幫助我如何從android中提取網址數據。如何獲得Json數組從Android網站的網址?
1
A
回答
1
call JSONLoadTask()。execute(yourUrl);
public class JSONLoadTask extends AsyncTask<String, Void, JSONObject>{
@Override
protected void onPreExecute() {
super.onPreExecute();
//your code
}
@Override
protected JSONObject doInBackground(String... params) {
JSONParser jParser = new JSONParser();
JSONObject object = jParser.getJsonObject(params[0]);
return object;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
//get the values from result here
}
}
}
採取以下類[從URL Android上檢索JSON]的
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
/**
* This method is used to parse the Url by taking the url as parameter
*
* @param url
* @return
*/
public Object getJsonObject(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
httpGet.setParams(httpParams);
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
json = EntityUtils.toString(httpEntity);
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
if (json.startsWith("[")) {
// We have a JSONArray
try {
jObj = new JSONObject();
jObj.put("data", new JSONArray(json));
} catch (JSONException e) {
Log.d("JSON Parser",
"Error parsing JSONArray " + e.toString());
}
return jObj;
}
// return JSON String
return jObj;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
}
+0
謝謝哈雷什,我試過這種方法,但無法得到答案,因爲網站URL以ip格式顯示。 –
+0
更新了json解析的代碼 –
+0
謝謝haareesh,我會試試。是這段代碼將網頁轉換爲json格式,例如,如果我的網址是www.stackoverflow.com –
相關問題
- 1. 如何從json獲取數據網址?
- 2. 如何獲取網站的網址?
- 3. 如何從網頁的網址中提取網站的網址?
- 4. 從facebook獲得Android的規範網址
- 5. WordPress的:如何獲得網站的網址
- 6. 如何從一個網站的網址
- 7. 從網站網址
- 8. 如何從谷歌網站頁面獲取網址參數
- 9. 如何獲得網址參數的JavaScript
- 10. 後如何獲得網址「:」
- 11. 如何從句子中獲得網址?
- 12. 如何從網址獲得評論Facebook?
- 13. 從aspx獲取JSON網址
- 14. 如何從網站獲得輸入?
- 15. 如何獲得從網站API
- 16. 如何從網站獲得服務
- 17. 如何從Android中的壓縮網址獲得完整的URL?
- 18. 如何從網站獲取包含關鍵字的網址?
- 19. 如何從網站獲取所有有效的網址?
- 20. 流星:如何從Facebook網址獲得JSON
- 21. 如何從wordpress網站獲得json響應
- 22. 從JSON數組生成靜態網站
- 23. 從網址獲取數組的值
- 24. 如何從GoogleSource網站獲取android Telephony.java
- 25. 從外部網站獲取JSON數據
- 26. 如何從網站獲取mp3音頻流媒體網址?
- 27. 如何從整個文本中獲取網站網址?
- 28. 如何使用JSON在android的網站上獲取數據?
- 29. Android從網址獲取特定的Json數據到變量
- 30. 如何獲得一個網址使用漂亮的網址?
可能重複(http://stackoverflow.com/questions/5577857/retrieving-json-from-url-on -android) – atok
非常感謝你,我會試試這個。這是可能創建我們自己的按鈕和文本框來檢查結果。我是新手到Android –