我正在尝试从URL获取搜索结果
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON
并使用 log.v()
将其显示在LogCat中 .
问题是,而不是在 LogCat
中显示搜索结果,程序不停地运行,我在 LogCat
中看到的是一个无穷无尽的列表
11-19 15:51:11.256:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%释放6651K / 7688K,暂停8ms,总计8ms 11-19 15:51:11.264:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%免费6651K / 7688K,暂停8ms,总计8ms 11-19 15:51:11.272:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%免费6651K / 7688K,暂停5ms,总计5ms 11-19 15:51:11.272:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%免费6651K / 7688K,暂停4ms,总计5ms 11-19 15:51:11.324:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14 %免费6651K / 7688K,暂停45ms,总计45ms 11-19 15:51:11.332:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%免费6651K / 7688K,暂停8ms,总计8ms 11-19 15:51: 11.400:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%释放6651K / 7688K,暂停23ms,总计23ms 11-19 15:51:11.424:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%免费6651K / 7688K,暂停2ms,总计2ms 11-19 15:51:11.464:D / dalvikvm(1251):GC_FOR_ALLOC释放480K,14%释放6651K / 7688K,暂停26ms,总计26ms
这永远不会停止并继续,直到我在模拟器中杀死应用程序 .
这是我的代码
公共类MainActivity扩展Activity {
public static final String TAG= MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isNetworkAvilable()){
GetSearchData getSearchData = new GetSearchData();
getSearchData.execute();
}
else{
Toast.makeText(this, "The network is down", Toast.LENGTH_SHORT).show();
}
}
private boolean isNetworkAvilable() {
// TODO Auto-generated method stub
boolean isAvailable= false;
ConnectivityManager manager= (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=manager.getActiveNetworkInfo();
if(networkInfo !=null && networkInfo.isConnected()){
isAvailable=true;
}
return isAvailable;
}
private class GetSearchData extends AsyncTask<Object, Void, String>{
protected String doInBackground(Object... params){
int responseCode=-1;
try{
URL url=new URL("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=JSON");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
responseCode= connection.getResponseCode();
// Log.i(TAG, "Code: "+responseCode);
if(responseCode== HttpURLConnection.HTTP_OK){
InputStream inputStream= connection.getInputStream();
Reader reader= new InputStreamReader(inputStream);
int nextCharacter;
String responseData="";
while(true){
nextCharacter=reader.read();
if(responseCode==-1){
break;
}
responseData+= (char) nextCharacter;
}
JSONObject jsonObject= new JSONObject(responseData);
JSONObject jsonObject2= jsonObject.getJSONObject("responseData");
// JSONObject jsonObject3= jsonObject.getJSONObject("results");
JSONArray jsonArray= jsonObject2.getJSONArray("results");
for(int i=0; i<jsonArray.length(); i++){
JSONObject results = jsonArray.getJSONObject(i);
String title= results.getString("title");
Log.v(TAG, "Result "+i+" : "+title);
}
}
else{
Log.i(TAG, "Wrong Response Code: " + responseCode);
}
}
catch(MalformedURLException e){
Log.e(TAG, "Exception: "+e);
}
catch(IOException e){
Log.e(TAG, "Exception: "+e);
}
catch(Exception e){
Log.e(TAG, "Exception: "+e);
}
return "Code: "+responseCode;
}
}
}
1 回答
尝试使用此代码进行网络呼叫,它应该可以正常工作,至少从互联网上获取数据,然后将其发布到LogCat中 .