2011-03-11 35 views
0

嗨在我的應用程序我發送請求到服務器。首先調用它給了我空值,然後它給了我價值。爲什麼我得到null請幫助我。我的代碼是。 HttpUtilities類包含請求方法。Android從服務器給予空值

public class WebUtilities 
{ 
private static HttpClient client = new DefaultHttpClient();  
private static HttpGet request; 
public static String getRequest(String strURL) 
{ 
    String Result = " "; 
    request = new HttpGet(strURL); 
    try 
    { 
     HttpResponse response = client.execute(request); 
     if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
      Result = new HttpUtilities().request(response); 
     else 
      Result = null; 
    } 
    catch (Exception ex) 
    { 
     Result = null; 
    } 
    return Result; 
} 
} 
public String request(HttpResponse response) 
{ 
    try 
    { 
     InputStream in = response.getEntity().getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     StringBuilder str = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      str.append(line); 
      str.append("\n"); 
     } 
     in.close(); 

     result = str.toString(); 

    } 
    catch (Exception ex) 
    { 
     result = ""; 
    } 
    return result; 
} 

回答

0

使用

Result = EntityUtils.toString(response.getEntity()) 

,而不是

Result = new HttpUtilities().request(response); 
嘗試