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

标签 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

请帮助。谢谢

最佳答案

您可以通过以下方式调用此任务

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;
    }

}

关于android - 如何将输入传递到android中的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172346/

相关文章:

ios - 让 indexPath = NSIndexPath(forRow : 0, inSection: 0) 的替代方法

android - 以编程方式将自定义 View 添加到 fragment 布局

java - 空检查不起作用

ios - CLLocation Manager 委托(delegate)问题

iphone - 当应用程序处于后台时,不在应用程序委托(delegate)中调用 didReceiveLocalNotification 方法 [iOS6]

iphone - 在运行时获取 CPU 使用率 | iOS

ios - 整个模块优化 - 将 pod 中的所有 swift 文件放在主项目中?

java - Android 对话框和内存

Android:覆盖方法时出错

android - 如何动态地将字符串消息添加到 Android 屏幕