如何使用Android处理ASP.NET发布方法?

时间:2019-04-12 04:44:44

标签: android asp.net

我正在使用android调用asp.net方法。我已使用HTTPClient调用此api,但未获得预期的输出。还有其他方法可以调用此api,请帮帮我。

URL:     http://xxx.yyy.com.np/pay.aspx

示例发布数据:

<form action="http://xxx.yyy.com.np/pay.aspx" method="post">
    <input type="hidden" value="2001" name="ID">
    <input type="hidden" value="user_name" name="Username">
</form>

源代码:

 private void postDetailToServer() {
    try {

        // url where the data will be posted
        String postReceiverUrl = "http://xxx.yyy.com.np/pay.aspx";
        Log.v(TAG, "postURL: " + postReceiverUrl);

        // HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(postReceiverUrl);

        // add your data
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        nameValuePairs.add(new BasicNameValuePair("ID", "111"));
        nameValuePairs.add(new BasicNameValuePair("Username","name"));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // execute HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {

            String responseStr = EntityUtils.toString(resEntity).trim();
            Log.v(TAG, "Response: " + responseStr);

        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案