我试图以 JSON形式将查询结果发送到我的查询,然后创建其JSON对象并对其执行某些操作. 但我无法收到回复:( 我尝试了两种方法. 任何人都可以帮我解决问题吗? Approach1: URL searhURL;//St
但我无法收到回复:(
我尝试了两种方法.
任何人都可以帮我解决问题吗?
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
如您所见,我尝试使用API密钥而不使用API密钥.但仍有问题在线评论为问题在这里.
方法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();
}
这就是为什么我们建议你总是包含一个logcat输出,因为如果没有它我们就无法想象它.
将其添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
你得到一个SecurityException,因为你没有告诉Android你正在使用网络.
