我觉得我这个逻辑没有问题,但是无法显示那个菜品详情
我设定的是点击back按钮,就运行异步操作显示菜品信息,具体代码如下:
package com.lexieluv.homeworkthirteenth; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import com.lexieluv.homeworkthirteenth.adapter.MyAdapter; import com.lexieluv.homeworkthirteenth.entity.DetailBean; import com.lexieluv.homeworkthirteenth.entity.FoodBean; import com.lexieluv.homeworkthirteenth.util.JsonUtils; import com.lexieluv.homeworkthirteenth.util.NetUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import image.SmartImage; import image.SmartImageView; import static com.lexieluv.homeworkthirteenth.util.JsonUtils.parseJson; public class FoodActivity extends AppCompatActivity { private TextView name; private SmartImageView img; private TextView originalprice; private TextView tPrice; private TextView price; private TextView description; private ImageView back; int i = 1; String u = "http://www.imooc.com/api/shopping?type=12"; @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.description); init(); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AsynctaskSecond(FoodActivity.this).execute(); Log.d("**************", String.valueOf(i)); i++; } }); } //在主线程初始化绑定 private void init() { name = findViewById(R.id.tv_title_second); img = findViewById(R.id.iv_food_second); originalprice = findViewById(R.id.tv_price_first); tPrice = findViewById(R.id.tv_price_second); price = findViewById(R.id.tv_price_third); description = findViewById(R.id.tv_description); back = findViewById(R.id.back_main); } //异步任务类下载网络数据的类 class AsynctaskSecond extends AsyncTask<Void, Integer, Boolean> { Context context; AsynctaskSecond(Context context){ this.context = context; } @Override protected Boolean doInBackground(Void... voids) { return true; } @Override protected void onProgressUpdate(Integer... values) { Gson g = new Gson(); DetailBean detailBean = g.fromJson(Net.getData(u).toString(),DetailBean.class); name.setText(detailBean.getName()); img.setImageUrl(detailBean.getImg()); originalprice.setText(String.valueOf(detailBean.getOriginalprice())); tPrice.setText(detailBean.getTPrice()); price.setText(detailBean.getPrice()); description.setText(detailBean.getDescription()); } } } class Net{ public static StringBuilder getData(String urlString) { HttpURLConnection connection = null; BufferedReader reader = null; StringBuilder response = null; try { URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); //对获取的输入流进行读取 reader = new BufferedReader(new InputStreamReader(in)); response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect(); } } return response; } }
0
收起
正在回答
3回答
你这啥都没做,你让它显示啥呀?
LexieMIZUKI
2019-05-29 21:06:54
我发现jsonString的内容是可以打印出来的,但是不知道是不是因为只有一条object,所以打印jsonArray的length的时候,什么也没有显示,按道理应该显示1。
package com.lexieluv.homeworkthirteenth; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.lexieluv.homeworkthirteenth.adapter.DetailAdapter; import com.lexieluv.homeworkthirteenth.entity.DetailBean; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class FoodActivity extends AppCompatActivity { ImageView back; static String url = "http://www.imooc.com/api/shopping?type=12"; ListView no; int i = 0; @Override public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.no); back = findViewById(R.id.back_main); no = findViewById(R.id.no); // AsynctaskSecond asyncTask = new AsynctaskSecond(FoodActivity.this,no); // asyncTask.execute(u); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AsynctaskSecond(FoodActivity.this,no).execute(url); Log.d("**************", String.valueOf(i)); i++; } }); } } //异步任务类下载网络数据的类 class AsynctaskSecond extends AsyncTask<String, Void, List<DetailBean>> { //传入一个context,为了Myadapter的执行 private Context context; ListView no; public AsynctaskSecond(Context context,ListView no) { super(); this.context=context; this.no = no; } //将URL对应的JSON格式数据转化为我们所封装的DetailBean对象 private List<DetailBean> getJsonData(String url) { List<DetailBean> list=new ArrayList<DetailBean>(); try { String jsonString=readStream(new URL(url).openStream()); //调试是否返回正确json格式的数据字符串 Log.i("info2", "getJsonData:"+jsonString); JSONObject jsonObject; DetailBean detailBean; //通过传入json格式的数据字符串,用JSON内置函数解析字符串,得到json数据 jsonObject=new JSONObject(jsonString); Log.d("***************333", jsonObject.toString()); //data就是我们在浏览器看到的data数组,不要把data写成了date,刚就犯了这个错,获取不到数据 JSONArray jsonArray=jsonObject.getJSONArray("data"); Log.d("***************111", String.valueOf(jsonArray.length())); //得到整个data数组后,我们就可以通过遍历数组,得到数组里每个一个对象的值 for(int i=0;i<=jsonArray.length();i++){ jsonObject =jsonArray.getJSONObject(i); detailBean=new DetailBean(); detailBean.setName(jsonObject.getString("name")); detailBean.setDescription(jsonObject.getString("description")); detailBean.setOriginalprice(Double.parseDouble(jsonObject.getString("originalprice"))); detailBean.setPrice(jsonObject.getString("price")); detailBean.setTPrice(Integer.parseInt(jsonObject.getString("tPrice"))); detailBean.setImg(jsonObject.getString("img")); list.add(detailBean); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }catch (JSONException e) { e.printStackTrace(); } return list; } //通过URL获取网页返回的字符串信息 private String readStream(InputStream is) { InputStreamReader isr; String result=""; //一次性将URL的所有JSON格式数据都截取,赋值到result //获取的是一个JSON加密的格式,在浏览器访问的时候,系统会自动将乱码格式转换了正常的格式 try { String line=""; isr=new InputStreamReader(is,"utf-8"); BufferedReader br=new BufferedReader(isr); while((line=br.readLine())!=null){ result+=line; Log.i("info1", "line:"+line); Log.i("info1", "result:"+result); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return result; } @Override protected List<DetailBean> doInBackground(String... params) { return getJsonData(params[0]); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(List<DetailBean> list) { super.onPostExecute(list); DetailAdapter adapter=new DetailAdapter(context,list); no.setAdapter(adapter); Log.d("***************2",list.toString()); } }
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星