2013-06-21 4 views
1

내 문제를 해결하기 위해 연구를했지만 슬프게도 지금까지는 할 수 없었습니다. 그다지 큰 문제는 아니지만 나는 그것에 붙어 있습니다 ...HTTP 요청을 전달하는 키워드

Google과 같은 검색 엔진에서 일부 키워드로 검색해야합니다. 나는이 작업을 수행하려면 여기에 두 개의 클래스를 가지고 :

package com.sh.st; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class EventSearch extends SearchScreen implements ActionListener { 

    public EventSearch(){ 

     btsearch.addActionListener(this); 

    } 

     public void actionPerformed(ActionEvent e){ 

      if(e.getSource()==btsearch){ 
      String query=txtsearch.getText(); 
      } 

     } 



} 

package com.sh.st; 

import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

public class HttpRequest extends SearchScreen 
{ 
    URL url = new URL("google.com" + "?" + query).openConnection(); 
    URLConnection connection = url.openConnection(); 
    connection.setRequestProperty("Accept-Charset", "UTF-8"); //Possible Incompatibility 
    InputStream response = connection.getInputStream(); 

} 

그래서, txtsearch가 SearchScreen라는 이름의 또 다른 클래스에서 제공을하고 나는 하나의 문자열 명명 된 쿼리에 값을 때문. 나는 HttpRequest 클래스에 질의를 전달할 필요가있다. 그리고 이것을 확장하면된다. 나는 틀렸다는 것을 확신하지만 나는 이것을하는 다른 누군가를 보았다. 이것이 첫 번째 문제입니다. 어떻게해야합니까?

두 번째와 I 받고있어 가장 중요한 구문 오류 : 나는 완벽하게 캐릭터 세트를 수락 "(connection.setRequestProperty"의미와의 유틸리티를 이해하지 못했다

enter image description here enter image description here

"," UTF-8 ");"

  1. How to send HTTP request in java?
  2. : 나는 그것을 이해할 수 '물론 독서는 아마 내 요청에서 올하지만 구문 오류가 나는 등의 링크에서 연구를했다

    나를 위해 분명하지 않다하더라도됩니다 caracters에 관한 것입니다

  3. getting text from password field
  4. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
  5. Using java.net.URLConnection to fire and handle HTTP requests

모두 좋은 재료를 가지고 있지만 완전히 이해할 수는 없으며 따르려고하는 부분이 작동하지 않습니다. 누구든지 제발 도와 주실 수 있습니까?

편집 :

+0

텍스트 – Raedwald

+0

으로 구문 오류 신고 해주세요 (의견 인라인) 주먹 클래스가 아닌 – NullPointerException

+0

에 어떤 방법으로 코드를 넣어 @ NullPointerException이 문법 문제는, 고마워요, 지금부터 자바 전문가가 아니기 때문에 저의 실수를 저지르는 것이 일반적입니다. 하지만 클래스 간의 문자열 전달 부분이 여전히 누락되었습니다 ... –

답변

1

이 코드를 사용해보십시오 [주제는 해결] :

// Fixed search URL; drop openConnection() at the end 
URL url = new URL("http://google.com/search?q=" + query); 

// Setup connection properties (this doesn't open the connection) 
URLConnection connection = url.openConnection(); 
connection.setRequestProperty("Accept-Charset", "UTF-8"); 

// Actually, open the HTTP connection 
connection.connect(); 

// Setup a reader 
BufferedReader reader = new BufferedReader(
         new InputStreamReader(connection.getInputStream())); 

// Read line by line 
String line = null; 
while ((line = reader.readLine()) != null) { 
    System.out.println (line); 
} 

// Close connection 
reader.close(); 
관련 문제