【发布时间】:2016-07-24 01:27:30
【问题描述】:
这是我用来获取值的方法。
@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;
}
我想使用 obj.getMake() 等,来自 JSON 的值。但是不明白怎么做,还是退货吧。我知道应该是返回值,或者使用final。
【问题讨论】:
-
在 AsyncTask 类中实现 onPostExecute 方法 :) 顾名思义,一旦 doInBackground 完成执行,就会调用它。从 onPostExecute 调用您要调用的方法并将您在 onPostExecute 中收到的响应作为参数传递:) 就是这样:)
标签: android methods android-asynctask return-value