2014-12-31 56 views
1

我正在開發大學在線結果檢查android應用程序,以便學生在文本框中輸入他/她的註冊號碼結果應該顯示。我有大學的網址,但我不知道如何提取json格式的網址,並將其稱爲android請任何人幫助我如何從android中提取網址數據。如何獲得Json數組從Android網站的網址?

+1

可能重複(http://stackoverflow.com/questions/5577857/retrieving-json-from-url-on -android) – atok

+0

非常感謝你,我會試試這個。這是可能創建我們自己的按鈕和文本框來檢查結果。我是新手到Android –

回答

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 –