2012-10-16 2 views
0

http 요청을 보내고 해당 요청에서 문자열을 반환하고 싶습니다.URL에서 텍스트를 가져 오는 Android가 항상 NULL입니다.

현재이 코드를 사용하고 있지만 URL이 무엇이든 관계없이 항상 null이 표시됩니다. 그게 무슨 문제 야?

시도 { // 원하는 페이지 URL의 URL에 대한 URL = 새 URL을 생성 ("HTTP : // 호스트 이름 : 80/index.html을");

// Read all the text returned by the server 
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
String str; 
while ((str = in.readLine()) != null) { 
    // str is one line of text; readLine() strips the newline character(s) 
} 
in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } 
+0

코드 것은 잘 작동 적절한 URL을 제공하십시오. . Android에서이 작업을 수행하려고 할 때 AndroidManifest.xml에서 적절한 인터넷 권한을 설정 했습니까? – Darwind

+0

AndroidManifest.xml에 인터넷 사용 권한을 추가했습니다. 나는 적절한 URL을주지 만, 매번 null을 얻는다. – user1319668

+0

당신은 어떤 URL을 제공하고 있습니까? – Darwind

답변

0
   String URL = "http://hostname:80/index.html" 
       String XML = stringWriter.toString(); 
       // System.out.println(XML); 

       se = new StringEntity(XML, "UTF-8"); 
       se.setContentType("text/xml;charset=UTF-8"); 
       HttpPost httppost = new HttpPost(URL); 
       httppost.setEntity(se); 

       HttpClient httpclient = new DefaultHttpClient(); 
       response = httpclient.execute(httppost); 

       HttpEntity entity = response.getEntity(); 

시도의 사용을 시도이 나를 위해 노력하고 있습니다 : 당신이 경우

httpClient = AndroidHttpClient.newInstance("Android"); 
     String url = "Your URL"; 
     try{ 
      HttpGet httpGet = new HttpGet(url); 
      HttpResponse response = httpClient.execute(httpGet); 
      if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //Don't know what string you are waiting for 
       //do something 
      } 
     } 
     catch(IOException e){ 
      httpClient.close(); 
     } 
+0

불행히도 작동하지 않습니다 – user1319668

+0

사실 URL이 잘못되었습니다. 이제이를 고치고 "java.io.EOFException"예외가 발생합니다. 물론 이것은 원래 코드로 테스트되었습니다. – user1319668

0

에서이

URL url = new URL("http://whatever"); 
URLConnection urlConnection = url.openConnection(); 
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
//read stream here  
+0

불행히도 작동하지 않습니다 – user1319668

+0

"HttpResponse response = httpClient.execute (httpGet);"오류가 발생합니다. – user1319668

+0

어떤 종류의 오류가 있습니까? 좀 더 구체적 일 수 있습니까? – zkminusck

관련 문제