Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Bing api sample code

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Bing api sample code

    Hi
    here is a code for bing api search.
    my problem is in line :
    sb.indexOf("<d:Description")
    what dose this line do?

    correct answer is below:
    http://stackoverflow.com/questions/2...-from-bing-api

      public static void main(String[] args) {
               String searchText = "searchtext";
               searchText = searchText.replaceAll(" ", "%20");
               String accountKey="key-ID";
     
            byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
            String accountKeyEnc = new String(accountKeyBytes);
            URL url;
            try {
                url = new URL(
                        "https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27Web%27&Query=%27" + searchText + "%27&$format=JSON");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
     
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));
            StringBuilder sb = new StringBuilder();
            String output;
            System.out.println("Output from Server .... \n");
            //write json to string sb
            while ((output = br.readLine()) != null) {
     
                sb.append(output);
     
            }
     
            conn.disconnect();
             //find webtotal among output      
           int find= sb.indexOf("\"WebTotal\":\"");
           int startindex = find + 12;
     
     
           int lastindex = sb.indexOf("\",\"WebOffset\"");
     
            System.out.println(sb.substring(startindex,lastindex));
     
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
     
                e.printStackTrace();
            }
     
     
        }
    Last edited by saeedeh; June 17th, 2014 at 11:42 PM. Reason: found the answer


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Bing api sample code

    my problem is
    What is the problem? Copy the full text of any error messages and post it here.

    what dose this line do
    Have you read the API doc for the class to see what that method does?
    If you have questions about what the API doc says, copy it and paste it here with your questions.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bing api sample code

    I want to access the number of search result for a string using bing api.
    i have key for using this api.
    know i have problem in using this api.
    from below link
    http://stackoverflow.com/questions/1...-from-bing-api

    I know I should use webtotal.
    d->results[0]->WebTotal

    but I don't know how to use this line in java.
    here is about using bing api:
    http://social.msdn.microsoft.com/For...rum=DataMarket
    Last edited by saeedeh; June 17th, 2014 at 11:08 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Bing api sample code

    Do you get any errors? Please copy the full text of the error message and paste it here.

    Is there a forum for the bing api? I have never seen any bing packages.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bing api sample code

    no there was not any error.

    but this code is not complete. this code only connect to bing and search a word. but i want to access to webtotal in the result. how can i access to this part from result? and how can i access to result?

    No I don't think there is a forum for this packages.
    here is a about bing search api:
    Bing Search API | Windows Azure Marketplace

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Bing api sample code

    Sorry, I know nothing about the bing api. Maybe someone else here knows of it.
    If you have a java programming problem post it here.
    If you are having problems with someone else's code, ask the author of that code about the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2014
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bing api sample code

    I found the answer and correct the question in first post.

Similar Threads

  1. Is this code an API? Beginner learning to write an API and decoupling.
    By Learning2Java in forum Java Theory & Questions
    Replies: 3
    Last Post: April 22nd, 2014, 12:59 PM
  2. Decode sample code
    By Cronus in forum Java Theory & Questions
    Replies: 1
    Last Post: July 17th, 2013, 12:24 PM
  3. I need help (Sample code)
    By AGBESI in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 06:10 AM
  4. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM
  5. Java application using C++.Also provide some links and sample code
    By jazz2k8 in forum Java Native Interface
    Replies: 5
    Last Post: July 23rd, 2008, 06:01 AM

Tags for this Thread