Java 在android应用程序中使用http post
我需要在我的应用程序中执行简单的http post 找到示例并创建了Java 在android应用程序中使用http post,java,android,http-post,httpclient,Java,Android,Http Post,Httpclient,我需要在我的应用程序中执行简单的http post 找到示例并创建了AsyncTask类。这篇文章的主要代码如下: nameValuePairs-是post元素 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL_STRING); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
AsyncTask
类。这篇文章的主要代码如下:
nameValuePairs
-是post元素
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 post,我可以试试:)
谢谢大家的帮助。异常
org.apache.http.client.HttpResponseException
发出非2xx http响应信号,如下所述:
您可以使用简单的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
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://Your URL/”;
试试{
//添加您的数据
List nameValuePairs=新的ArrayList(3);
添加(新的BasicNameValuePair(“Name1”、“Value1”);
添加(新的BasicNameValuePair(“Name2”、“Value2”);
添加(新的BasicNameValuePair(“Name3”、“Value3”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}
捕获(客户端协议例外e)
{
//TODO自动生成的捕捉块
}
捕获(IOE异常)
{
//TODO自动生成的捕捉块
}
您是否已将INTERNET权限添加到menifest文件中?