3

I am trying to get bing results to my query in JSON form and then make its JSON object and perform somethings on it.

But I am not able to receive the response :(

I tried following two approaches.

Can anybody help me in resolving problem?

Approach1:

URL searhURL;
//String imageurl = "http://api.bing.net/json.aspx?AppID="+myBingAppID+"&Query="+formattedQuery+"&Sources=images";
String imageurl="http://www.bing.com/images/search?q="+formattedQuery+"&go=&qs=n&form=QBLH&filt=all";

    URL url = new URL(imageurl);
    InputStream is = null;

      URLConnection connection = url.openConnection();
      Log.d(LOGGER, "1.0");
        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream())); //problem is here
        Log.d(LOGGER, "2.0");
        while ((line = reader.readLine()) != null) {
                builder.append(line);
        }
        Log.d(LOGGER, "3.0");

         jSONString = builder.toString();
        //after cutting off the junk, its ok
        Log.d(LOGGER, "String is : "+jSONString);
        JSONObject jSONObject = new JSONObject(jSONString); //whole json object

As you can see, I tried with both using API key and without using API key. But still problem at line commented as problem is here.

Approach 2

         String jsonString="";
        try {  
               URL url = new URL(imageurl);  
               Log.d(LOGGER, "1");
               BufferedReader in = new BufferedReader(new InputStreamReader(  
                 url.openStream())); //problem here 
               Log.d(LOGGER, "2");
               String inputLine;  

               while ((inputLine = in.readLine()) != null) {  
                //JSON data get stored as a string  
                jsonString = inputLine;  

               }  
               Log.d(LOGGER, "3");
               in.close();  
               Log.d(LOGGER, "String is : "+jsonString);
}
        catch (IOException e) {  
               e.printStackTrace();  
              }  
3
  • log statements beyond the buffer reader are not displayed in log cat. And also jsonString is not made/displayed Commented Feb 11, 2012 at 19:04
  • justpaste.it/qf4 is the exact log cat statement. Commented Feb 11, 2012 at 21:28
  • I didn't understand your question but maybe this will be useful for you. java2s.com/Code/Android/Network/RestClient.htm Commented Feb 11, 2012 at 21:30

1 Answer 1

7

And that's why we suggest you always include a logcat output since there's no way we could have figured it out without it.

Add this to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

You're getting a SecurityException because you haven't told Android that you're using the network.

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

1 Comment

Thanks a lot ! This helped me :) I absolutely forgot to add this permission.

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.