net_whiz
net_whiz

Reputation: 135

How to obtain result from a website using Http in android

I want to use an external website http://www.siirretytnumerot.fi/ in my android application. This website takes in two values PREFIX and NUMBER. I am confused at the moment as I seem not to be getting any output in my textview. I dont know what to use whether httpget or httppost. I tried both and still no result. But when i go to the link for explorer and enter an input the website line changes to http://www.siirretytnumerot.fi/QueryServlet. I have tried to use both still no output. Please can someone help me look through the website and suggest which of the http methods is correct for me to use? here is the code I used.

 TextView tv=(TextView)findViewById(R.id.display);
  try {
    HttpClient client = new DefaultHttpClient();  
    String postURL = "http://www.siirretytnumerot.fi/";
    HttpPost post = new HttpPost(postURL); 
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("PREFIX", "044"));
        params.add(new BasicNameValuePair("NUMBER", "9782231"));
        params.add(new BasicNameValuePair("LANGUAGE", "Finnish"));
        params.add(new BasicNameValuePair("Submit", "Hae"));
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);  
        HttpEntity resEntity = responsePOST.getEntity();  
        if (resEntity != null) {    
            tv.setText(EntityUtils.toString(resEntity));
        }
} catch (Exception e) {
    e.printStackTrace();
}

the output from the link comes out as an image source

<img src="QueryServlet?ID=-7187780920186056107&amp;STRING=5WQAy%2BQCUZRGIUJ8qZtpSrmkiKzWp8HRL7Ti1xmFSxMAEZE7GHEtaylOApMGd9qoesY7Pl%2BUN1Z6Kzap9RIg%2Bw==" />

Now how do i read this?

Upvotes: 1

Views: 364

Answers (2)

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23952

Use developer tools of browser to find out which fields are required.

You should make POST request to http://www.siirretytnumerot.fi/QueryServlet

Try to add these fields to your request:

Submit: Hae
LANGUAGE: Finnish

I hope it'll help

EDIT

It will look like that

TextView tv=(TextView)findViewById(R.id.display);
  try {
    HttpClient client = new DefaultHttpClient();  
    String postURL = "http://www.siirretytnumerot.fi/";
    HttpPost post = new HttpPost(postURL); 
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("PREFIX", "044"));
        params.add(new BasicNameValuePair("NUMBER", "9782231"));

        //here the new lines
        params.add(new BasicNameValuePair("LANGUAGE", "Finnish"));
        params.add(new BasicNameValuePair("Submit", "Hae"));

        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);  
        HttpEntity resEntity = responsePOST.getEntity();  
        if (resEntity != null) {    
            tv.setText(EntityUtils.toString(resEntity));
        }
} catch (Exception e) {
    e.printStackTrace();
}

Upvotes: 1

Sankalp
Sankalp

Reputation: 1128

You can view the source and there is a line

<input type="hidden" name="LANGUAGE" value="Finnish">

So you need to add that field too,as they might be using it.So

 params.add(new BasicNameValuePair("LANGUAGE", "Finnish"));

Upvotes: 1

Related Questions