Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12695

HTML parsing in Android

I have one link

http://devappandroid.com/Android_App/test_uploada%20.html

on that i am uploading A Image and as a response i am getting file with content

and I want to read this file

my code is here

final String feedUrlString = "http://www.icoke.com.tw/iCoke/summer2011/jsp/upload.html";
    String sen;
    Element e;
    DocumentBuilderFactory docBuilderFactory;
    DocumentBuilder docBuilder;
    Document doc;
    URL url;
    docBuilderFactory = DocumentBuilderFactory.newInstance();
    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
        url = new URL(feedUrlString);
        doc = docBuilder.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();
        e = doc.getDocumentElement();
        NodeList nl = e.getElementsByTagName("data");
        Element data = (Element) nl.item(0);
        sen = data.getAttribute("value");
        System.out.println(sen);
    } catch (Exception ex) {
        System.out.println("Error");
    }

But i cannot read that file please help me, Thanks.

Upvotes: 0

Views: 171

Answers (1)

Mac
Mac

Reputation: 146

try this.... it may useful.

you may need to download libraries from here (download HttpClient 4.1-Beta1 zip file)

try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Bitmap bm = BitmapFactory.decodeFile("/sdcard/image name.jpg");
        bm.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://www.icoke.com.tw/iCoke/summer2011/jsp/upload.html");
        ByteArrayBody bab = new ByteArrayBody(data, "icon.jpg");
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("uploaded", bab);
        reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        Log.e("executeMultipartPost : ", "" + s);
    } catch (Exception e) {
        // handle exception here
        Log.e("Exception ",e.getClass().getName() +" - "+ e.getMessage());
    }

Upvotes: 1

Related Questions