zoukankan      html  css  js  c++  java
  • 安卓解析json

    重点是开启网络权限

    难点是调用函数

    开启网络权限

        </application>
    <uses-permission android:name="android.permission.INTERNET"> 
    </uses-permission> 
    </manifest>

    读json的函数

    public static String get(String urlString)  {
            
            /*try{
                HttpGet request = new HttpGet(urlString);
                String result=getHttpClient().execute(request,new BasicResponseHandler());
                return result;
            }catch(IOException e){
                throw e;
            }*/
            
            String result="";
            BufferedReader in=null;
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet request=new HttpGet(urlString);
                HttpResponse response = client.execute(request);
                
                in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb=new StringBuffer("");
                String line="";
                String NL=System.getProperty("line.separator");
               // String NL="";
                while((line=in.readLine())!=null){
                    sb.append(line+NL);
                }
                in.close();
                result=sb.toString();
                result=JsonFilter(result);
            } 
            catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                if(in!=null){
                    try{
                        in.close();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
            return result;        
        }
        
        /*
         * 对json字符串进行过滤,防止乱码和黑框
         */
        public static String JsonFilter(String jsonstr){
            return jsonstr.substring(jsonstr.indexOf("{")).replace("
    ","
    "); 
        }

    调用函数

    /*****************************************************主要是点击事件*********************************************/     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
              final Button button1 = (Button) findViewById(R.id.button1);//final获得句柄 获得句柄
              final   TextView textView =  (TextView) findViewById(R.id.textView1);
                      
          
              button1.setOnClickListener(new OnClickListener()
              { 
                 public void onClick(View v)
                 {
                    
                     textView.setText(get("http://www.weather.com.cn/data/cityinfo/101010100.html"));
                     
    
                         }
                       
                 
              });
     
        }
  • 相关阅读:
    POJ 1860 Currency Exchange (Bellman ford)
    POJ 1502 MPI Maelstrom (最短路)
    2015 Multi-University Training Contest 2 1006(DFS)
    HDU 1495 非常可乐(枚举+DFS)
    HDU 5289 Assignment(单调队列)
    ACDream 1734 Can you make a water problem?(贪心)
    ACDream 1735 输油管道
    ACDream 1726 A Math game (折半查找)
    CSU 1602 Needle Throwing Game (投针问题)
    CSU 1604 SunnyPig (BFS)
  • 原文地址:https://www.cnblogs.com/hellowzd/p/4228445.html
Copyright © 2011-2022 走看看