Reputation:
This is the method which I use to get values.
@Override
protected Void doInBackground(String... params) {
try {
Intent intent = getIntent();
String dvlaNumFin = intent.getStringExtra("dvlaNumber");
final TextView outputView = (TextView) findViewById(R.id.showOutput);
final URL url = new URL("https://dvlasearch.appspot.com/DvlaSearch?licencePlate="+dvlaNumFin+"&apikey=");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
final StringBuilder output = new StringBuilder(String.valueOf(url));
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder responseOutput = new StringBuilder();
System.out.println("output===============" + br);
while ((line = br.readLine()) != null) {
responseOutput.append(line);
}
br.close();
HandleJSON obj = new HandleJSON("");
obj.readAndParseJSON(responseOutput.toString());
output.append(System.getProperty("line.separator") + "\n" + System.getProperty("line.separator") + "Make : " + obj.getMake() + "\nModel : " + obj.getModel());
output.append("\nSix Month Rate : " + obj.getSixMonthRate() + "\nTwelve Month Rate : " + obj.getTwelveMonthRate() + "\nDate of First Registration : " + obj.getDateofFirstRegistrationegistration());
output.append("\nYear of Manufacture : " + obj.getYearOfManufacture() + "\nCylinder Capacity : " + obj.getCylinderCapacity() + "\nCO2 Emmissions : " + obj.getCo2Emissions());
output.append("\nVIN number : " + obj.getVin() + "\nTransmission type : " + obj.getTransmission());
DVLAresult.this.runOnUiThread(new Runnable() {
@Override
public void run() {
outputView.setText(output);
progress.dismiss();
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
I would like to use obj.getMake() and so on, the values from JSON. But do not understand how to do it, or return it. I know should be return value, or by using final.
Upvotes: 0
Views: 1948
Reputation: 2121
Nice and simple. Make your AsyncTask return a value:
public class TestClass extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(String... params) {
//rest of code
return output.toString();
}
}
Now all you have to do is call .get() method after calling .execute()
TestClass tc = new TestClass();
tc.execute();
String output = tc.get();
Very Very Important Note
By calling .get() right after .execute() your UI thread will be blocked until the AsyncTask is done. This is counter intuitive to the purpose of AsyncTask. One of the solutions to this problem is adding a callback interface to the AsyncTask which will be called upon finishing and call the .get() method in the implementation of that interface. For an example on how to design a callback interface see here.
Upvotes: 1
Reputation: 20379
FOG
Simply implement onPostExecute inside your AsyncTask class :)
for example :
@Override
protected void onPostExecute(String makeValue) {
// remember this method gets called on main thread
letsCallFogsMethod(makeValue); //call your method and pass the make value here :)
}
Thats it buddy :) Now how come this onPostExecute is getting any value??? You have to return it from doInBackground method dude :)
like
@Override
protected String doInBackground(String... params) {
//after all bra bla simply say
return obj.getMake();
}
Do you notice any change in your doInBackground signature buddy?? Yeah I changed from Void to String :)
By writing String you are informing that when you are done executing doInBackground you will return a string to onPostExecute :)
So if I write as it is in the answer will it work ?? Nope. Given that you have specified Void in your doInBackground your Async task signature might look something like
private class FogsAsyncTask extends AsyncTask<bla,blah,Void> {
Can you see the last Void??? :) But now you have chnaged doInBackground isn't it so update the AsyncTask signature as well :)
private class FogsAsyncTask extends AsyncTask<bla,blah,String> {
Now it should work fine :) Happy coding buddy :) Hope my answer helped you :)
Upvotes: 0
Reputation: 933
AsyncTask
has three (main) methods, onPreExecute
, doInBackground
and onPostExecute
. Only doInBackGround
runs on a background thread, the other two run on the UI thread. (there is also onProgressUpdate
but I will skip it here)
In your case, return anything you want in doInBackground
method. That return value will be the input param for onPostExecute
. There you can call any other (reachable) method you want. Mind that you'd be running inside the UI thread at that time.
Upvotes: 0
Reputation: 537
You can get the output on onPostExecute method just override the method and get the output on it
Upvotes: 0