java - Android java跨域

标签 java android http post request

我正在为android制作一个应用程序,我需要使用java向www.example.com地址发出请求。问题是我需要发送 POST 参数,我一直在搜索一些信息,我发现了一些关于跨域或类似的东西。有人可以帮我解决请求并得到答案吗?

我尝试执行以下代码但不起作用:

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://www.example.com");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("param1", "val"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

解决方案:::::::::::::

  try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://www.example.com");

                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

                postParameters.add(new BasicNameValuePair("key", "val"));
                postParameters.add(new BasicNameValuePair("key2", "val2"));


                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                        postParameters);
                post.setEntity(formEntity);

                    HttpResponse response = client.execute(post);
                        //inputStreamToString method
                    String data = inputStreamToString(response.getEntity()
                            .getContent());
                    return data;
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

inputStreamToString方法

private String inputStreamToString(InputStream is) {
        String s = "";
        String line = "";

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                s += line;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Return full string
        return s;
    }

最佳答案

跨域 POST 适用于 Web 浏览器提交表单,而不适用于一般 HTTP。作为安全策略,浏览器不会跨域发布。它不适用于您的情况。

关于java - Android java跨域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778307/

相关文章:

android - windowSoftInputMode : adjustResize doesn't work properly for numeric keyboard, 当键盘提示文本时

http - 什么是 HTTP 响应实体标签?

java - 在方法内使用binarySearch

java - MicroStream(反序列化)如何工作?

java - 为什么我的 Button Image setGraphic 方法没有显示?

android - 不了解在两台设备上使用预安装的 android 从另一台设备管理 android 设备的工作流程

java - 命令是否从 shell 脚本中的当前目录运行?

java - 从post请求中直接获取json信息

php - 使用 PHP 检查远程页面是否存在?

http - 微服务应如何处理409响应?