2017-05-30 31 views
0

我嘗試閱讀從PHP代碼生成的xml字符串的web內容。這裏的result。但它保持我的應用程序崩潰。當t嘗試閱讀www.yahoo.com時也是如此。Android:讀取xml字符串的網頁內容

這裏是我的代碼

public static void downloadString(String urlString,String saveLoc, String fileName){ 
     URL url; 
     try { 
      url = new URL(urlString); 
      //URLConnection conn = url.openConnection(); 
      HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
      int code = conn.getResponseCode(); 

      BufferedReader br = new BufferedReader(
        new InputStreamReader(conn.getInputStream())); 

      String inputLine; 

      String fullFileName = saveLoc + "//" + fileName; 
      File file = new File(fullFileName); 

      if (!file.exists()) file.createNewFile(); 

      FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
      BufferedWriter bw = new BufferedWriter(fw); 

      while ((inputLine = br.readLine()) != null) {bw.write(inputLine);} 

      bw.close(); 
      br.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

我嘗試調試和不能從

INT碼= conn.getResponseCode通過();

我不知道什麼確切的錯誤。請指教。謝謝。

回答

0

我會建議你使用排球庫。

在build.gradel文件中添加這兩種依賴性:

compile 'com.google.code.gson:gson:2.2.+' 
compile 'com.android.volley:volley:1.0.0' 

然後添加到您的Java文件:`

 StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String s) { 

      gson = new Gson(); 
      s=s.substring(10,s.indexOf(']')+1)+"}]"; 
      Log.d("Error",s); 
      list = (List) gson.fromJson(s, List.class); 
      postTitle = new String[list.size()]; 

      for(int i=0;i<list.size();++i){ 
       mapPost = (Map<String,Object>)list.get(i); 
       //mapTitle = (Map<String, Object>) mapPost.get("url"); 
       postTitle[i] = (String) mapPost.get("url"); 


       // ArrayList<String> list= (ArrayList<String>) mapPost.get("categories"); 

       // postTitle[i] = mapPost.get("categories"); 

       //postTitle[i] = TextUtils.join(", ",(ArrayList<String>) mapPost.get("url")); 

      } 

      postList.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,postTitle)); 
      progressDialog.dismiss(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      Toast.makeText(MainActivity.this, "Some error occurred", Toast.LENGTH_LONG).show(); 
     } 
    }); 

    RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this); 
    rQueue.add(request); 




// get the total post 

//mapPost = (Map<String,Object>)list.get(i); 


//get Post Title 

//mapTitle = (Map<String, Object>) mapPost.get("title"); 

// postTitle[i] = (String) mapTitle.get("rendered"); 


//get post id 

//to string: postTitle[i] = Integer.toString((( 
Double)mapPost.get("id")).intValue()); 

//to integer: int postID = ((Double)mapPost.get("id")).intValue(); 


//get post date 

//postTitle[i] = (String) mapPost.get("date"); 

//post link 
//postTitle[i] = (String) mapPost.get("link"); 

//get post author 
//to string: postTitle[i] = Integer.toString((( 
Double)mapPost.get("author")).intValue()); 
//to integer: int postID = ((Double)mapPost.get("author")).intValue(); 

我也建議你看到凌空一些教程。

我解析URL是這樣的:Bing Wallpaper Image.xml

+0

對不起,我真的不知道這個......如何使用這段代碼? –

+0

https://www.simplifiedcoding.net/wordpress-to-android-app-tutorial/看到這個網頁。 –

0

添加這兩條線。

public static void downloadString(String urlString,String saveLoc, String fileName){ 
     URL url; 
     try { 
      url = new URL(urlString); 
    HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
    conn.setRequestMethod("GET"); //this lines 
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");//this lines 
    int code = conn.getResponseCode(); 
    BufferedReader br = new BufferedReader(
        new InputStreamReader(conn.getInputStream())); 

      String inputLine; 

      String fullFileName = saveLoc + "//" + fileName; 
      File file = new File(fullFileName); 

      if (!file.exists()) file.createNewFile(); 

      FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
      BufferedWriter bw = new BufferedWriter(fw); 

      while ((inputLine = br.readLine()) != null) {bw.write(inputLine);} 

      bw.close(); 
      br.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

仍然錯誤,或者我錯過了 <使用權限android:name =「android.permission.READ_EXTERNAL_STORAGE」/> <使用權限android:name =「android.permission.INTERNET」/>

+0

你給INTERNET permisssion? –

+0

是的,我也嘗試另一種方式,如[鏈接](https://stackoverflow.com/questions/15442546/get-error-in-http-url-connection)其不工作...任何建議請 –