繁体   English   中英

在Android应用中使用http发布

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM