2013-02-03 41 views
0

我遇到以下代碼嚴重的問題,並且會很感激任何形式的幫助。Android登錄屏幕變空白

public class WebHandler 
{ 
private String username; 
private String password; 
private boolean errorOccured; 
private String errorMessage; 
private String server; 

public WebHandler(String server) 
{ 
    this.server = server; 
    StrictMode.ThreadPolicy policy = new 
      StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
} 

public void setUsername(String username) 
{ 
    this.username = username; 
} 

public void setPassword(String password) 
{ 
    this.password = password; 
} 

public String getInternetData(String url) 
{ 
    BufferedReader bufferedReader = null; 
    String data = null; 

    try 
    { 
     HttpClient client = new DefaultHttpClient(); 
     URI website = new URI("http://google.com"); 
     HttpGet request = new HttpGet(); 
     request.setURI(website); 
     HttpResponse response = client.execute(request); 
     bufferedReader = new BufferedReader(new  
        InputStreamReader(response.getEntity().getContent())); 

     StringBuffer stringBuffer = new StringBuffer(""); 

     String line; 

     while ((line = bufferedReader.readLine()) != null) 
     { 
      stringBuffer.append(line); 
      Log.e("PLW", line); 
     } 

     bufferedReader.close(); 
     data = stringBuffer.toString(); 
    } 
    catch(Exception e) 
    { 
     this.errorOccured = true; 
     this.errorMessage = e.getMessage(); 
    } 

    return data; 

} 

public String Post(List<NameValuePair> nameValuePair, String url) 
{ 
    String result = null; 

    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost("http://www.google.com/"); 

    nameValuePair.add(new BasicNameValuePair("usernmae", this.username)); 
    nameValuePair.add(new BasicNameValuePair("password", this.password)); 

    Log.e("PLW_0", "called post..."); 

    try 
    { 
     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
     HttpResponse response = httpClient.execute(httpPost); 
     result = response.toString(); 
     Log.e("PLW_1", "response:" + result); 
    } 
    catch (Exception e) 
    { 
     // writing exception to log 
     Log.e("PLW_2", "Error:", e); 
     Log.e("PLW_3", "error:" + e.getMessage()); 
    } 
    return result; 
} 

public boolean hasError() 
{ 
    return this.errorOccured; 
} 

public String getError() 
{ 
    return this.errorMessage; 
} 
} 
+4

能否請你描述你所面臨的 '嚴重的問題'?你是否遇到異常?加載時間過長嗎? – Brandon

回答

0

您是否試圖在其他線程中運行您的代碼? 避免使用連接或耗時的任務阻塞主線程。

更新時間:

的AsyncTask例子:

  1. http://anujarosha.wordpress.com/2012/01/27/handling-http-post-method-in-android/
  2. http://pcfandroid.wordpress.com/2011/07/14/http-post-with-asynctask-android-tutorial/
+0

是的,那是我最初做的。不幸的是,屏幕只是空白,所以我GOOGLE了它,發現互聯網訪問沒有啓用默認情況下在Android上,所以我不得不使用下面的代碼,這是拋出一個新的一套錯誤信息: \t \t StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); \t \t StrictMode.setThreadPolicy(policy); – user2038123

+0

註釋掉** StrictMode語句**並再次運行代碼後,我得到此行所拋出的異常'HttpPost httpPost = new HttpPost(「http://www.google.com/」);'調用這個通過這個語句在我的登錄類'WebHandler處理程序=新的WebHandler(「192.xxx」);列表 nameValuePairs =新ArrayList ();字符串結果=處理程序.Post(nameValuePairs,「test.txt」) ;'。因此,test.txt只是一個包含** Hello World **的文件。請問我在這裏做錯了什麼? – user2038123

+0

是什麼例外?操作系統版本的測試? 你有沒有試過把你的連接放在AsyncTask中? 示例: 1. http://anujarosha.wordpress.com/2012/01/27/handling-http-post-method-in-android/ 2. http://pcfandroid.wordpress.com/2011/07/14/http-post-with-asynctask-android-tutorial/ – madlymad