class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
try {
postData(uri[0]);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
myTask2.cancel(true);
}
@Override
protected void onPostExecute(String url) {
LinearLayouts();
}
}
public static String postData(String url) throws UnsupportedEncodingException {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
String responseString = "";
try {
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
InputStream inputStream = null;
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
inputStream = response.getEntity().getContent();
if(inputStream != null)
responseString = convertInputStreamToString(inputStream);
else
responseString = "Did not work!";
JSONObject reader = null;
JSONArray sys = null;
try {
reader = new JSONObject(responseString);
String res_metar = reader.getString("YOUR TAG");
} catch (JSONException e) {
e.printStackTrace();
}
//a = responseString;
} else{
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
// process execption
}
return responseString;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
You should write it to top in your class.
private static RequestTask myTask2;
and add it to your mainactivity,
myTask2=(RequestTask)new RequestTask().execute(url);