【问题标题】:how to pass inputs into url in android如何在android中将输入传递给url
【发布时间】:2013-11-24 20:59:18
【问题描述】:

我开发了一个 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"));

最终的url应该是:xxxxxxxx/loadhtml.php?where=DeliverySystem&what=LoginCheck.php&un=usertest&pwd=1234567890

请帮忙。谢谢

【问题讨论】:

    标签: android ios


    【解决方案1】:

    你可以调用这个任务

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

    【讨论】:

    • 知道为什么我当前的代码是错误的吗?我似乎也无法生成 url 以在控制台中查看它。
    猜你喜欢
    • 1970-01-01
    • 2014-07-21
    • 2012-10-03
    • 2022-07-25
    • 2020-04-05
    • 2013-01-11
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多