我想得到这个网页的文件的内容。
http://weather.gtimg.cn/city/0101021403.js,
试了好多方法,返回的都是乱码或者空/谁能帮我
 这个是qq获得天气情况的代码。
 我以前代码是
public static String geturlget(String turl, String encode)
                         throws IOException {
                 URL url = new URL(turl);
                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                 conn.setRequestMethod("GET");
                 conn.setConnectTimeout(5 * 1000);                 InputStream input = conn.getInputStream();
                 InputStreamReader inputReader = new InputStreamReader(input, "gbk");//gbk改成utf-8或者不写,都是乱码
                BufferedReader reader = new BufferedReader(inputReader);                 String inputLine = null;                 StringBuffer sb = new StringBuffer();
                 while ((inputLine = reader.readLine()) != null) {
                         sb.append(inputLine);
                 }
                 reader.close();
                 inputReader.close();
                 input.close();
                 conn.disconnect();
                 return sb.toString();
         }

解决方案 »

  1.   


    public void getJsonDataFromServer(String urlPath, String Args, String cmd) throws Exception{
        
         StringBuffer json=new StringBuffer();
        URL url = null;
        HttpsURLConnection urlCon = null;
        BufferedReader in = null;
       
         if(cmd.equals("POST")){
                url = new URL(urlPath);
                urlCon = (HttpsURLConnection) url.openConnection();
                urlCon.setReadTimeout(20000);
                   urlCon.setConnectTimeout(5000);
                   urlCon.setDoOutput(true);
                   urlCon.setDoInput(true);
                   urlCon.setRequestMethod("POST");
                   urlCon.setUseCaches(false);
                   DataOutputStream osw = new DataOutputStream(urlCon.getOutputStream());
                   osw.writeBytes(Args);
                   osw.flush();
                   osw.close();
         }
         else if(cmd.equals("GET"))
         {
        
                url = new URL(urlPath);
                urlCon = (HttpsURLConnection) url.openConnection();
                urlCon.setReadTimeout(20000);
                   urlCon.setConnectTimeout(5000);
                   urlCon.setDoOutput(true);
                   urlCon.setDoInput(true);
         }
                 
         int ReCode = 0;        
         ReCode = urlCon.getResponseCode();
    //           Log.i(TAG, " getJsonData():urlCon.getResponseCode()================"+ ReCode);
               if(ReCode!=200){
                 in = new BufferedReader(new InputStreamReader(urlCon.getErrorStream(),"UTF-8"));
                }else{
                 in = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"UTF-8"));
                }
               String line;
               while((line = in.readLine()) != null) {
                   json.append(line);
               }        if(null != urlCon){
             urlCon.disconnect();
             urlCon = null;
            }
           in.close();
    }你试试,参数你自己可以看着填
      

  2.   

    public static String get(String turl, String encode) throws IOException { StringBuffer json = new StringBuffer();
    URL url = new URL(turl);
    HttpsURLConnection urlCon = (HttpsURLConnection) url.openConnection();/////////执行到此句,报错
    BufferedReader in = null; urlCon.setReadTimeout(20000);
    urlCon.setConnectTimeout(5000);
    urlCon.setDoOutput(true);
    urlCon.setDoInput(true); int ReCode = 0;
    ReCode = urlCon.getResponseCode();
    if (ReCode != 200) {
    in = new BufferedReader(new InputStreamReader(
    urlCon.getErrorStream(), "UTF-8"));
    } else {
    in = new BufferedReader(new InputStreamReader(
    urlCon.getInputStream(), "UTF-8"));
    }
    String line;
    while ((line = in.readLine()) != null) {
    json.append(line);
    } if (null != urlCon) {
    urlCon.disconnect();
    urlCon = null;
    }
    in.close();
    return json.toString(); }