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"));
                     
    
                         }
                       
                 
              });
     
        }
  • 相关阅读:
    Let和Const的使用
    Spring框架学习10——JDBC Template 实现数据库操作
    python 学习
    delphi
    mysql 客户端连接报错Illegal mix of collations for operation
    tnsping 不通
    orm总结
    other
    resultset 查询时返回多个相同值
    vlan 知识学习
  • 原文地址:https://www.cnblogs.com/hellowzd/p/4228445.html
Copyright © 2011-2022 走看看