2017-05-30 33 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)其不工作...任何建议请 –