简体   繁体   English

带有发布数据的android HTTP请求

[英]android HTTP request with post data

Alright so I have the following situation, I got a java app, where users can login, once they do it will generate a random key, I wish to send this key to my website using http requests. 好了,我遇到以下情况,我有一个Java应用程序,用户可以登录,一旦用户登录,它将生成一个随机密钥,我希望使用http请求将此密钥发送到我的网站。

On the web side I will insert it into the database for later use, this is already working. 在Web端,我将其插入数据库中以备后用,这已经在起作用。

Now at this moment I am very stuck with the java side. 现在,在这一刻,我非常拘泥于Java方面。 I am using the following code to try the http request: 我正在使用以下代码尝试http请求:

private void startRegistration(String username, String password) {

    String myRandomString = randomString(6);
    String deviceId = "abc";
    startToast(myRandomString);

    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        insertDevice(myRandomString, deviceId);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@TargetApi(Build.VERSION_CODES.KITKAT)
public void insertDevice(String randomString, String deviceId) throws IOException {

//        String urlParameters  = "key=" + randomString + "&device_id="+ deviceId;
//        byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
//        int    postDataLength = postData.length;
    String request        = "http://website.com/test";
    URL    url            = new URL( request );

    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setDoOutput( true );
        conn.setInstanceFollowRedirects( false );
        conn.setRequestMethod( "GET" );
        conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty( "charset", "utf-8");
//            conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
        conn.setUseCaches( false );

    try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
//            wr.write( postData );
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, "test", duration);
        toast.show();
       }
   }

On the web side I have a route set to catch this request and simple throws a log::info so I can check if the request even ever gets there, this looks like follow: 在网络端,我设置了一个路由来捕获此请求,并简单地抛出log :: info,因此我可以检查请求是否到达那里,如下所示:

public function create() {

    Log::info('I got here, hurray!');
}

now when I hit the URL in my browser I can see the log appearing, when I use my app, I don't. 现在,当我在浏览器中单击URL时,可以看到日志出现,而当我使用应用程序时却没有。 But there are no errors thrown either. 但是,也没有抛出任何错误。

At the end I'd like to use a POST request and send both the random key, and device ID to the website. 最后,我想使用POST请求并将随机密钥和设备ID发送到网站。 any help is much appreciated 任何帮助深表感谢

You are setting up the connection but you're not triggering the request. 您正在建立连接,但没有触发请求。 You can perform the request by calling conn.connect() or "operations that depend on being connected" ( javadoc ) 您可以通过调用conn.connect()或“依赖于连接的操作”( javadoc )来执行请求。

Here's an example of a POST request: 这是POST请求的示例:

public void insertDevice(String randomString, String deviceId) {

    String request = "http://website.com/test";
    URL url = new URL(request);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestMethod("POST");

    String urlParameters = "key=" + randomString + "&device_id=" + deviceId;
    byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

    conn.setDoOutput(true);

    try (OutputStream os = conn.getOutputStream()) {
        os.write(postData);
    }

    conn.connect();

    // Get response code, e.g. 200.
    // conn.getResponseCode();

    // Read server response data with conn.getInputStream() or conn.getErrorStream().

    conn.disconnect();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM