0

我正在尝试使用名称值键对通过 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) {}
}


} 
4

3 回答 3

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);
}
于 2012-09-12T02:17:39.597 回答
1

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

  1. 检查互联网许可
  2. 检查 api 是否真的使用 Rest Client 工作
  3. 检查网址是否正确
  4. 检查端口号是否正确
  5. 检查所需的标题
于 2012-09-12T03:48:21.160 回答
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

于 2012-09-12T01:27:08.260 回答