public class Window extends Service { public void onCreate() { // TODO Auto-generated method stub super.onCreate(); HttpRequest request = new HttpRequest(); String json = request.execute("url").toString(); txtPackageName.setText(json); //txtPackageName - is a TextView }
public class HttpRequest extends AsyncTask<String,Void, JSONArray> { @Override protected JSONArray doInBackground(String... params) { URL url; HttpURLConnection urlConnection = null; JSONArray response = new JSONArray(); try { url = new URL(params[0]); urlConnection = (HttpURLConnection) url.openConnection(); int responseCode = urlConnection.getResponseCode(); String responseString = readStream(urlConnection.getInputStream()); Log.v("CatalogClient", responseString); response = new JSONArray(responseString); /* if(responseCode == HttpStatus.SC_OK){ String responseString = readStream(urlConnection.getInputStream()); Log.v("CatalogClient", responseString); response = new JSONArray(responseString); }else{ Log.v("CatalogClient", "Response code:"+ responseCode); }*/ } catch (Exception e) { e.printStackTrace(); } finally { if(urlConnection != null) urlConnection.disconnect(); } return response; } private String readStream(InputStream in) { BufferedReader reader = null; StringBuffer response = new StringBuffer(); try { reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { response.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return response.toString(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)