2011-02-15 9 views
1

이 코드를 사용하여 웹 서버에 HttpGet 요청을합니다. 에뮬레이터에서는 제대로 작동하지만 HTC Sense에서는 작동하지 않습니다. excecution은 http 요청을하지 않고 끝납니다. 어떤 생각?HttpClient가 실제 장치에서 작동하지 않습니다.

File f = new File(user.getPhotopath()); 
List<NameValuePair> reqparams = new LinkedList<NameValuePair>(); 
reqparams.add(new BasicNameValuePair("email", user.getEmail())); 
reqparams.add(new BasicNameValuePair("pwd", user.getPassword())); 
reqparams.add(new BasicNameValuePair("name", user.getScreenName())); 
reqparams.add(new BasicNameValuePair("photo", f.getName())); 
reqparams.add(new BasicNameValuePair("preference", user.getPrefs())); 
reqparams.add(new BasicNameValuePair("bluetoothid", bid)); 

String urlstring = "http://www.mysite.com/me?"+ URLEncodedUtils.format(reqparams, "utf-8"); 

try { 
    URL url = new URL(urlstring); 

URI myURI = null; 
try { 
    myURI = url.toURI(); 
} catch (URISyntaxException e) { 
       e.printStackTrace(); 
} 
HttpClient httpClient = new DefaultHttpClient(); 
HttpGet getMethod = new HttpGet(myURI); 
HttpResponse webServerResponse = null; 
HttpEntity httpEntity = null; 
try { 
    webServerResponse = httpClient.execute(getMethod); 
    httpEntity = webServerResponse.getEntity(); 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
if (httpEntity != null) { 

    InputStream instream = httpEntity.getContent(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
     try { 
      resultStr = sb.toString(); 
      instream.close(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
    } 
+0

stacktrace를 표시하십시오. – Falmarri

+0

실제로 스택 추적이 도움이 될뿐만 아니라 에뮬레이터가 프록시에서 작동하는지 여부도 도움이됩니다. – ndrix

답변

0

접근 방법이 정확하고 효과가 있지만 EntityUtils 도우미 클래스를 사용하여 응답 본문을 문자열로 검색하는 것이 훨씬 쉽습니다. 간단하게 :

String body = EntityUtils.toString(httpEntity); 
+0

고마워, 나는 xD가 존재한다는 것을 몰랐다. –

+0

나도 마찬가지다. 비슷한 방법을 쓰고 나면 그것을 발견했다. –

0

stacktrace에는 예외 나 경고가 없었습니다. 메모리 할당 메시지 만. 어쨌든 나는 HttpClient 대신 URLConnection을 사용하여 그것을 해결했다 :

try { 
     URL url = new URL(urlstring); 
     URLConnection connection = url.openConnection(); 
     InputStream inputStream = connection.getInputStream(); 
     BufferedInputStream bufferedInput = new BufferedInputStream(inputStream); 

    // Read the response into a byte array 
     ByteArrayBuffer byteArray = new ByteArrayBuffer(50); 
     int current = 0; 
     while((current = bufferedInput.read()) != -1){ 
      byteArray.append((byte)current); 
     } 

     // Construct a String object from the byte array containing the response 
    resultStr = new String(byteArray.toByteArray()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

무엇인가의 이유로. 여전히 잘못 된 것을 파악하려고합니다.

+0

답장을 보내 주셔서 감사합니다. –

관련 문제