[英]Using http post in android app
我需要在我的應用中進行簡單的http發布。
找到示例並創建AsyncTask
類。 執行發布的主要代碼是這樣的:
nameValuePairs
是發布元素
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL_STRING);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
String data = new BasicResponseHandler().handleResponse(response);
我怎么得到這個例外
org.apache.http.client.HttpResponseException: Forbidden
這是什么意思? 如果該服務返回了某些內容,那么如何查看完整消息?
另外,如果還有其他方法可以使http發布,我可以嘗試一下:)
謝謝你們的幫助。
異常org.apache.http.client.HttpResponseException
表示非2xx HTTP響應,如下所示: http : //hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpResponseException .html 。
您可以使用以下簡單的httpPOST方法:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://Your URL/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Name1", "Value1"));
nameValuePairs.add(new BasicNameValuePair("Name2", "Value2"));
nameValuePairs.add(new BasicNameValuePair("Name3", "Value3"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.