0

I try to read web content of xml string where produce from PHP Code. Here the result. But its keep my application crash. It's same when t tried to read www.yahoo.com too.

Here's my code

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();
        }
    }

I tried to debug and cannot pass from

int code = conn.getResponseCode();

I dont know what exacty the error. Please advice. Thanks.

2 Answers 2

0

I would recommend you to use volley library.

Add these two dependencies in your build.gradel file:

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

And then add this to your java file:`

        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();  

I would also recommend you to see some tutorials on volley.

My parsing URL was this: Bing Wallpaper Image.xml

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, i dont really get this...how to use this code ?
0

add this two lines.

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();
        }
    }

3 Comments

still error, or i miss something beside <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name = "android.permission.ACCESS_NETWORK_STATE"/>
did you give INTERNET permisssion ??
Yes, i also try another way like link its doesn;t work too... any advice please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.