关于3-2作业的问题
public class Json extends AsyncTask<String, Void, String> {
private FoodRegister mFood;
public FoodRegister getFood() {
return mFood;
}
@Override
protected void onPostExecute(String s) {
mFood = new FoodRegister();
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
int mint = jsonObject.getInt("status");
String mstring = jsonObject.getString("msg");
JSONObject data = jsonObject.getJSONObject("data");
String sName = data.getString("name");
mFood.setName(sName);
mFood.setDescription(data.getString("description"));
mFood.setImg(data.getString("img"));
mFood.setOriginalprice(data.getDouble("originalprice"));
mFood.setPrice(data.getDouble("price"));
mFood.settPrice(data.getInt("tPrice"));
/**
* 数据都得到了,不知道怎么传回到FoodActivity????
*
*/
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL("http://www.imooc.com/api/shopping?type=12");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
inputStreamReader.close();
bufferedReader.close();
return stringBuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星