Reputation: 553
I use this sample code of bing api :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import org.jsoup.Jsoup;
public class bingSearch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//--------------------------------------Bing search------------------------------
String searchText = "swim";
searchText = searchText.replaceAll(" ", "%20");
String accountKey="Your-AccountKEY";
byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
String accountKeyEnc = new String(accountKeyBytes);
URL url;
try {
url = new URL(
"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=%27" + searchText + "%27&$top=50&$format=Atom");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
// conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
System.out.println("Output from Server .... \n");
char[] buffer = new char[4096];
while ((output = br.readLine()) != null) {
sb.append(output);
// text.append(link + "\n\n\n");//Will print the google search links
//}
}
conn.disconnect();
int find = sb.indexOf("<d:Description");
int total = find + 1000;
System.out.println("Find index: " + find);
System.out.println("Total index: " + total);
sb.getChars(find+35, total, buffer, 0);
String str = new String(buffer);
int find2 = str.indexOf("</d:Description>");
int total2 = find2 + 400;
System.out.println("Find index: " + find);
System.out.println("Total index: " + total);
char[] buffer2 = new char[1024];
str.getChars(0, find2, buffer2 , 0);
String str2 = new String(buffer2);
str2 = Jsoup.parse(str2).text();
System.out.println(str2);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The out put is:
Output from Server ....
Find index: 1014
Total index: 2014
Find index: 1014
Total index: 2014
A computer is a general purpose device that can be programmed to carry out a finite set of arithmetic or logical operations. Since a sequence of operations can be ...
It show just one result, But I need more than one result. Doing it with this code is possible? Or Is other substitute for this code? Thanks
Upvotes: 1
Views: 2477
Reputation: 44911
In your call to Bing you are requesting the results as an Atom feed, and that's what you're getting back (38785 chars long for that particular query) and you really should treat it as an Atom feed and parse it in a more appropriate way.
The reason you only get one result in your code though is that you never seem to loop over the sb
string containing the feed. If you really want to parse the feed this way you need to move to code after conn.disconnect()
to a loop and use something like sb.indexOf("<d:Description", int fromIndex)
to traverse the string increasing the fromIndex each time you find a new match.
But you really should treat the response from Bing like the xml-feed it is and parse it using some xml-library, for instance Rome.
Upvotes: 1