2012-11-22 80 views
0

我正在使用將XML數據作爲Web服務的Android應用程序。 我的任務是通過使用HTTP Post發佈值並添加數據並在列表視圖中顯示來添加數據。雖然後我需要加密爲UTF-8格式。Http Post不能在Android中工作

下面我給了我的代碼。

private String postSyncXML() { 
    String url = strMainUrl + strSubUrl; 
    HttpClient httpclient = new DefaultHttpClient(); 


    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs 
      .add(new BasicNameValuePair("uid", strUniqueId)); 
    nameValuePairs.add(new BasicNameValuePair("first_name", strFirstName)); 
    nameValuePairs.add(new BasicNameValuePair("last_name", strLastName)); 
    nameValuePairs.add(new BasicNameValuePair("email", strEmail)); 
    nameValuePairs.add(new BasicNameValuePair("phone", strPhone)); 
    nameValuePairs.add(new BasicNameValuePair("notes", strNotes)); 
    nameValuePairs.add(new BasicNameValuePair("is_sms", strIsSms)); 
    nameValuePairs.add(new BasicNameValuePair("is_email", strIsEmail)); 


    UrlEncodedFormEntity form; 
    try { 
     form = new UrlEncodedFormEntity(nameValuePairs, "UTF-8"); 
     HttpPost httppost = new HttpPost(url); 
Log.d("", "Add Guest URL "+url); 
     httppost.setEntity(form); 

     HttpResponse response = (HttpResponse) httpclient.execute(httppost); 
     HttpEntity resEntity = response.getEntity(); 
     String resp = EntityUtils.toString(resEntity); 
     Log.i("Method call", "postSyncXML srv response:" + resp); 
     return resp; 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

在此先感謝。

+3

**不工作**那麼什麼實際發生的?拋出任何異常,如果是則輸出logcat。 –

+0

是的,它顯示錯誤,我在按鈕點擊時調用上述方法。因爲當我點擊按鈕時,所有通過此處傳送的數據都需要在網頁上發佈,然後我可以在網站上看到新增的數據。但是現在網站並未顯示新更新的數據。 – Dhamodharan

+0

錯誤是postSyncXML srv響應:錯誤 Dhamodharan

回答

0
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://192.168.192.131/"); 

try { 
    StringEntity se = new StringEntity("<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8); 
    se.setContentType("text/xml"); 
    httppost.setEntity(se); 

    HttpResponse httpresponse = httpclient.execute(httppost); 
    HttpEntity resEntity = httpresponse.getEntity(); 
    tvData.setText(EntityUtils.toString(resEntity)); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

Android, send and receive XML via HTTP POST method