无法通过HTTP POST发布

时间:2012-09-12 01:16:35

标签: android http-post

我正在尝试使用名称值密钥对通过HTTP Post发布数据。但是我无法发帖。帖子网址是http://mastercp.openweb.co.za/api/dbg_dump.asp。我应该在发帖时加入一些标题吗?谢谢

public class MainActivity extends Activity {
    Button ok;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
        ok=(Button)findViewById(R.id.but_signup_login);



        ok.setOnClickListener(new OnClickListener() {


            public void onClick(View arg0) {


                System.out.println("Clicked");
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://mastercp.openweb.co.za/api/dbg_dump.asp" });}});
    }






    public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://mastercp.openweb.co.za/api/dbg_dump.asp");

        System.out.println("Clicked again");
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(34);
            String amount ="Ashish";
            nameValuePairs.add(new BasicNameValuePair("User_Type", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Email", "ash@gmail.com"));
            nameValuePairs.add(new BasicNameValuePair("User_Email_In", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Pass", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Mobile", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Mobile_In", amount));
            nameValuePairs.add(new BasicNameValuePair("User_ADSL", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Org", amount));
            nameValuePairs.add(new BasicNameValuePair("User_VAT", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Name", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Surname", amount));
            nameValuePairs.add(new BasicNameValuePair("User_RegNo", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Address", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Town", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Code", amount));
            nameValuePairs.add(new BasicNameValuePair("User_State", amount));
            nameValuePairs.add(new BasicNameValuePair("User_Country", amount));
            nameValuePairs.add(new BasicNameValuePair("User_ADSL", amount));
            nameValuePairs.add(new BasicNameValuePair("User_ADSL_Address", amount));
            nameValuePairs.add(new BasicNameValuePair("Payment_CC_Alt", amount));
            nameValuePairs.add(new BasicNameValuePair("Payment_Type", amount));
             nameValuePairs.add(new BasicNameValuePair("CProfile", amount));
            nameValuePairs.add(new BasicNameValuePair("COrder", amount));
            nameValuePairs.add(new BasicNameValuePair("Debit_Name", amount));
            nameValuePairs.add(new BasicNameValuePair("Debit_Bank", amount));
            nameValuePairs.add(new BasicNameValuePair("Debit_Number", amount));
            nameValuePairs.add(new BasicNameValuePair("Debit_Code", amount));
            nameValuePairs.add(new BasicNameValuePair("Debit_Type", amount));
            nameValuePairs.add(new BasicNameValuePair("TOS_Agree", amount));  
            nameValuePairs.add(new BasicNameValuePair("Code", amount));  
            nameValuePairs.add(new BasicNameValuePair("package_activation", amount));  
            nameValuePairs.add(new BasicNameValuePair("session", amount)); 
            nameValuePairs.add(new BasicNameValuePair("OnceOff", amount));  
            nameValuePairs.add(new BasicNameValuePair("submit-button", amount));  


            try {
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            } catch (UnsupportedEncodingException e) {
               System.out.println("Unsupported Exception "+e);
                e.printStackTrace();
            }

        }  catch (Exception e) {
            System.out.println("  Exception last"+e);
            // TODO Auto-generated catch block
        }
    } 




    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
          String response = "";
          for (String url : urls) {


             postData(); 
          }


        return response;
      }
        @Override
        protected void onPostExecute(String result) {}
}


} 

3 个答案:

答案 0 :(得分:2)

您的postdata()方法实际上 帖子。您需要执行以下操作:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost, new BasicHttpContext());

然后您可以处理响应(首先检查response.getStatusLine().getStatusCode()的值)。例如,如果您希望响应中的字符串数据:

if (response.getStatusLine().getStatusCode() == 200) {
    BasicResponseHandler handler = new BasicResponseHandler();
    String data = handler.handleResponse(response);
}

答案 1 :(得分:1)

这是我关于实施Restful请求的个人清单:

  1. 检查互联网权限
  2. 检查api实际上是否可以使用Rest Client
  3. 检查网址是否正确
  4. 检查端口号是否正确
  5. 检查所需的标题

答案 2 :(得分:0)

这段代码给了我很多帮助:

       /**
        * HTTP post request
        *
        * @param sUrl
        * @param ht
        * @return
        * @throws Exception
        */
        public static HttpData post(String sUrl, Hashtable<String, String> ht) throws Exception {
                String key;
                StringBuffer data = new StringBuffer();
                Enumeration<String> keys = ht.keys();
                while (keys.hasMoreElements()) {
                        key = keys.nextElement();
                        data.append(URLEncoder.encode(key, "UTF-8"));
                        data.append("=");
                        data.append(URLEncoder.encode(ht.get(key), "UTF-8"));
                        data.append("&amp;");
                }
                return HttpRequest.post(sUrl, data.toString());
        }

点击此处查看完整课程:

http://moazzam-khan.com/blog/?p=490