如何将输入传递到android中的url

时间:2013-11-24 07:50:50

标签: android ios

我开发了一个ios应用程序,现在也可以作为Android应用程序实现。 我是android的新手,我遇到了url格式的问题。

我通过以下代码在ios中输入uname和pwd信息:

NSString *checkForLogin  = [NSString stringWithFormat:[[@"http://" stringByAppendingString:IP] stringByAppendingString: @"&what=LoginCheck.php&un=%@&pwd=%@" ],username.text,pwd.text];

对于android,我没有通过使用以下代码获得正确的最终URL:

nameValuePairs.add(new BasicNameValuePair("un", un.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("pwd", pwd.getText().toString()));
       HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(constants.URL + "&what=LoginCheckJava.php" );

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF_8"));

最终网址应为:xxxxxxxx / loadhtml.php?where = DeliverySystem& what = LoginCheck.php& un = usertest& pwd = 1234567890

请帮助。 THX

1 个答案:

答案 0 :(得分:0)

您可以使用

调用此任务
new PostMethodTask().execute("xxxxxxxx/loadhtml.php");

这个任务对你来说很好用

private class PostMethodTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response;
        HttpPost httpPost = new HttpPost(params[0]);

        String responseString = null;

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("where", "DeliverySystem"));
        nameValuePairs.add(new BasicNameValuePair("what", "LoginCheck.php"));
        nameValuePairs.add(new BasicNameValuePair("un", "usertest"));
        nameValuePairs.add(new BasicNameValuePair("pwd", "1234567890")); 

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            response = httpClient.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
                return responseString;
            } else {
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {

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

        return null;
    }

}