如何解决Android Studio上的HttpURLConnection连接问题

时间:2019-11-22 04:34:56

标签: java android httpurlconnection

我目前正在尝试在Android Studio上进行发布请求练习。我创建了一个按钮,当单击该按钮时,它应该从textview中获取一个数字,并将其作为参数传递给URL并得到响应。

我已经学会了如何自行执行发布请求,并且当我在Visual Studio Code上运行该请求时,我的代码也可以正常工作。 但是,当我在Android Studio上运行相同的代码时,则不会发生连接。

这是我的代码。

$('#btnAddToList').click(function () {
        item++;
        var customer_name = $('#customer_name').val();
        var producto_name = $('#producto_name').val();
        var product_price = $('#product_price').val();
        var product_stock = $('#product_stock').val();
        var product_quantity = $('#product_quantity').val();
        var Subtotal = product_price * product_quantity;

        var fila = '<tr><td>' + item + '</td><td>' + customer_name + '</td><td>' + producto_name + '</td><td>' + product_price + '</td><td>' + product_stock + '</td><td>' + product_quantity + '</td><td class="td-Subtotal">' + Subtotal + '</td></tr>';
        var btn = document.createElement('tr');
        btn.innerHTML = fila;
        document.getElementById('dtProduct').appendChild(btn);

        // initialize
        var total = 0;

        // loop
        $(".td-subtotal").each(function(){

          // get value
          total+=$(this).html();
        });

        // assign
        $("#total_pay").val(total);
    });

我用随机字符串替换了网址和参数。

问题发生在第Button translate = findViewById(R.id.transButton); translate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ TextView postText = findViewById(R.id.postText); String number = result.getText().toString(); String output = ""; HttpURLConnection connection = null; try { URL url = new URL("some random url that i replaced with"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept-Charset","UTF-8"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); String parameters = "random parameters"; OutputStream out = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(out, "UTF-8")); writer.write(parameters); writer.flush(); writer.close(); out.close(); int responseCode = connection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK){ output += "Status: True\n"; String line; InputStream in = new BufferedInputStream(connection.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); while ((line = reader.readLine()) != null) { String[] list = line.split(","); String[] message = list[2].split(":"); output += message[1]; } reader.close(); } else{ output += "Status: False\n"; } postText.setText(output); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage(output); builder.setTitle("Magic Number"); builder.setCancelable(true); builder.setPositiveButton( "Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } } ); AlertDialog alert = builder.create(); alert.show(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }); 行。我尝试通过更改textview文本来跟踪问题,发现连接从未发生过。同样,如果我在Visual Studio Code上运行完全相同的代码,它将连接并且可以从服务器获取响应,但是connection.connect()似乎不能在Android Studio上运行。

在有人问之前,我确实在清单文件中授予了Internet许可。

0 个答案:

没有答案