2014-09-06 75 views
-2

我想要構建一個應用程序,該應用程序接收用戶的短語,在網絡上搜索並在結果中顯示結果(因此它會更舒適地展示)。Android - 我如何開始網絡搜索並獲得結果到我的活動

到目前爲止,我設法啓動Google搜索,但無法將結果返回到我的活動。我試過StartActivityForResults,但似乎Google搜索不支持它。 我想以URL列表的形式或類似的東西獲得搜索結果,所以我可以在我的應用程序的另一個活動中打開它。有什麼建議麼?

這是到目前爲止我的代碼:

public void SearchWeb (View view) { 
    Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 
    EditText editText = (EditText) findViewById(R.id.edit_message); //get text from EditText 
    String query = editText.getText().toString(); 
    query = query.concat(" chords"); 
    intent.putExtra(SearchManager.QUERY, query); 
    if (intent.resolveActivity(getPackageManager()) != null) { 
     startActivity(intent); 
    } 
} 

回答

0

你做它的方式只允許你使用JSON

啓動搜索沒有得到任何結果回來 可以使用 CustomSearch Api從谷歌和分析結果

這是關於如何

public searchQuery { 
     String key="your API key"; 
     String qry="Android";// your keyword to search 
     URL url = new URL(
       "https://www.googleapis.com/customsearch/v1?    
       key="+key+"&cx="Replace with unique ID"&q="+ qry + 
       "&alt=json"); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Accept", "application/json"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(
       (conn.getInputStream()))); 

     String output; 
     System.out.println(url); 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 

      if(output.contains("\"link\": \"")){     
       String link=output.substring(output.indexOf("\"link\": \"")+ 
         ("\"link\": \"").length(), output.indexOf("\",")); 
       System.out.println(link); 
      }  
     } 
     conn.disconnect(); 
    } 
的片段
相關問題