Last active
December 18, 2015 05:39
-
-
Save tagrudev/5734523 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
["{\"id\":\"8626\",\"type\":\"БВ\",\"leaves\":\"03:34:00\",\"arrives\":\"06:11:00\"}","{\"id\":\"1620\",\"type\":\"БВ\",\"leaves\":\"06:00:00\",\"arrives\":\"08:40:00\"}","{\"id\":\"10110\",\"type\":\"ПВ\",\"leaves\":\"06:35:00\",\"arrives\":\"10:20:00\"}","{\"id\":\"1622\",\"type\":\"БВ\",\"leaves\":\"07:00:00\",\"arrives\":\"09:35:00\"}","{\"id\":\"8640\",\"type\":\"БВ\",\"leaves\":\"08:00:00\",\"arrives\":\"10:30:00\"}","{\"id\":\"10112\",\"type\":\"ПВ\",\"leaves\":\"12:00:00\",\"arrives\":\"15:27:00\"}","{\"id\":\"8610\",\"type\":\"БВ\",\"leaves\":\"12:20:00\",\"arrives\":\"14:51:00\"}","{\"id\":\"1624\",\"type\":\"БВ\",\"leaves\":\"13:00:00\",\"arrives\":\"15:40:00\"}","{\"id\":\"10114\",\"type\":\"ПВ\",\"leaves\":\"14:00:00\",\"arrives\":\"17:29:00\"}","{\"id\":\"8602\",\"type\":\"БВ\",\"leaves\":\"17:00:00\",\"arrives\":\"19:30:00\"}","{\"id\":\"10116\",\"type\":\"ПВ\",\"leaves\":\"17:15:00\",\"arrives\":\"20:28:00\"}","{\"id\":\"1626\",\"type\":\"БВ\",\"leaves\":\"18:00:00\",\"arrives\":\"20:38:00\"}","{\"id\":\"8612\",\"type\":\"RБВ\",\"leaves\":\"20:56:00\",\"arrives\":\"23:21:00\"}"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eu.appsbakery.bdz; | |
import android.os.Build; | |
import android.util.Log; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.SocketTimeoutException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
/** | |
* Created by harsh on 6/8/13. | |
*/ | |
public class WebServiceUtil { | |
public static JSONArray requestWebService(String serviceUrl) { | |
disableConnectionReuseIfNecessary(); | |
HttpURLConnection urlConnection = null; | |
try { | |
// create connection | |
URL urlToRequest = new URL(serviceUrl); | |
urlConnection = (HttpURLConnection) | |
urlToRequest.openConnection(); | |
urlConnection.setConnectTimeout(5000); | |
urlConnection.setReadTimeout(5000); | |
// handle issues | |
int statusCode = urlConnection.getResponseCode(); | |
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) { | |
// handle unauthorized (if service requires user login) | |
} else if (statusCode != HttpURLConnection.HTTP_OK) { | |
// handle any other errors, like 404, 500,.. | |
} | |
// create JSON object from content | |
InputStream in = new BufferedInputStream( | |
urlConnection.getInputStream()); | |
return new JSONArray(getResponseText(in)); | |
} catch (MalformedURLException e) { | |
// URL is invalid | |
} catch (SocketTimeoutException e) { | |
// data retrieval or connection timed out | |
} catch (IOException e) { | |
// could not read response body | |
// (could not create input stream) | |
} catch (JSONException e) { | |
// response body is no valid JSON string | |
} finally { | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
} | |
return null; | |
} | |
/** | |
* required in order to prevent issues in earlier Android version. | |
*/ | |
private static void disableConnectionReuseIfNecessary() { | |
// see HttpURLConnection API doc | |
if (Integer.parseInt(Build.VERSION.SDK) | |
< Build.VERSION_CODES.FROYO) { | |
System.setProperty("http.keepAlive", "false"); | |
} | |
} | |
private static String getResponseText(InputStream inStream) { | |
// very nice trick from | |
// http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html | |
return new Scanner(inStream).useDelimiter("\\A").next(); | |
} | |
public static List<Train> findAllItems() { | |
JSONArray serviceResult = WebServiceUtil.requestWebService( | |
"URL"); | |
List<Train> foundItems = new ArrayList<Train>(20); | |
try { | |
for (int i = 0; i < serviceResult.length(); i++) { | |
JSONObject obj = serviceResult.getJSONObject(i); | |
foundItems.add( | |
new Train(obj.getString("id"), | |
obj.getString("type"), | |
obj.getString("leaves"), | |
obj.getString("arrives"))); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return foundItems; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment