2014-03-13 38 views
0

我試圖顯示一個對話框,當應用程序啓動時有一些遠程數據(歡迎消息)。由於某種原因,這是行不通的。我的代碼是這樣的。Android啓動對話框遠程數據

p.s.我有另一個啓動時加載的進度類型對話框。我試圖在它之後添加它。

任何人都可以告訴這段代碼是否正確?

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.website.com/welcome.php"); 
try { 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
    nameValuePairs.add(new BasicNameValuePair("id", url)); 

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 

    ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
    String response = httpclient.execute(httppost, responseHandler); 
    //String url=response; 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("My Title"); 
    builder.setMessage(response); 
    builder.setPositiveButton("OK", null); 
    AlertDialog dialog = builder.show(); 
    TextView messageText = (TextView)dialog.findViewById(android.R.id.message); 
    messageText.setGravity(Gravity.CENTER); 


} catch (ClientProtocolException e) { 
    //Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show(); 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
//Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show(); 
// TODO Auto-generated catch block 
} 

回答

0

你可以做發生了什麼驗證碼DOS不行最好的測試是: 嘗試寫e.printStackTrace();之間捕獲像:

try { 
    ... 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

知道哇哈哈發生。

還我想你,你不能在UI線程訪問IO操作錯誤,你必須在其他線程LIKE調用這個方法:

private void MyMethod() { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.website.com/welcome.php"); 
    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
     nameValuePairs.add(new BasicNameValuePair("id", url)); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 

     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     final String response = httpclient.execute(httppost, responseHandler); 
     //String url=response; 

     runOnUiThread(
      new Runnable() { 
       @Override 
       public void run() { 
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder.setTitle("My Title"); 
        builder.setMessage(response); 
        builder.setPositiveButton("OK", null); 
        AlertDialog dialog = builder.show(); 
        TextView messageText = (TextView)dialog.findViewById(android.R.id.message); 
        messageText.setGravity(Gravity.CENTER); 
       } 
      } 
     ); 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

,並希望調用此方法時:

new Thread(new Runnable() { 
    @Override 
    public void run() { 
     MyMethod(); 
    } 
}).start(); 
+0

感謝您的代碼,得到了錯誤。對於一些愚蠢的錯誤,生產代碼有welcome.txt而不是welcome.php,並且你的代碼有助於獲得與http不允許相關的錯誤(就像我在做POST請求和.txt格式)。 – wmsgeek

0

句子「由於某種原因,這不起作用」是什麼意思?當我說對話沒有出現時,我是否正確?

  1. 存在httpclient.execute()的問題。如果互聯網連接出現問題,則不會創建對話框,因爲已經啓動了對話框。你能在Logcat中看到一些警告/錯誤嗎?
  2. 您不能在UI線程中執行連接操作,否則會引發NetworkOnMainThreadException
0

我要說不 - 該片段不正確。由於API 11所有IO功能都需要在後臺線程上發生。後臺線程無法觸及用戶界面。目前,您正在使用與創建AlertDialog相同的方法執行HttpPost。你需要做的是使用AsyncTask(1)在doInBackground方法中執行文章(2)返回結果(3)使用onPostExecute方法中的返回結果加載對話框(從doInBackground返回的值將自動發送它到onPostExecute)。 onPostExecute在UI線程上運行,因此您可以使用該方法創建一個AlertDialog