Visual Basic - Mostrar precio de Bitcoin en TextBox

Life is soft - evento anual de software empresarial
 
Vista:

Mostrar precio de Bitcoin en TextBox

Publicado por Baator (1 intervención) el 04/12/2022 07:24:21
Hola, qué tal. Necesito conseguir la forma de agregar el precio del BTC dentro de un TextBox que se actualice al darle a un botón. Hasta los momentos tengo este código pero me da error en:

"Dim data As String = response.GetResponseStream()" El valor de tipo Stream no se puede convertir en String.

"Dim jObject As JObject = jObject.Parse(content)" No está definido el tipo JObject. La variable jObject se usa antes de que se le haya asignado un valor.


Código completo para esa función:


Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader

' Set the URL of the API endpoint
Dim url As String = "https://api.coindesk.com/v1/bpi/currentprice.json"

Try
' Create a new HTTP to=python
request = HttpWebRequest.Create(url)

' Send the request and get the response
response = request.GetResponse()

' Read the response and store it in a string
Dim data As String = response.GetResponseStream()
Dim content As String = reader.ReadToEnd()

' Close the response
response.Close()

' Parse the JSON response to get the current Bitcoin price
Dim jObject As JObject = JObject.Parse(content)
Dim price As Double = jObject("bpi")("USD")("rate_float")

' Update the text box with the current Bitcoin price
TextBox1.Text = price

Catch ex As Exception
' Show an error message if the request failed
MessageBox.Show("Error: " & ex.Message)
End Try
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 145
Ha aumentado 1 puesto en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Mostrar precio de Bitcoin en TextBox

Publicado por Sah1d Ra (191 intervenciones) el 05/12/2022 19:11:53
hola

debes de analizar el texto de retorno y usar la funcion mid y instr

1
2
3
4
5
6
7
8
{"time":
     {"updated":"Dec 5, 2022 17:21:00 UTC","updatedISO":"2022-12-05T17:21:00+00:00","updateduk":"Dec 5, 2022 at 17:21 GMT"},
     "disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org","chartName":"Bitcoin","bpi":{
          "USD":{"code":"USD","symbol":"$","rate":"17,075.8750","description":"United States Dollar","rate_float":17075.875},
          "GBP":{"code":"GBP","symbol":"£","rate":"14,268.4645","description":"British Pound Sterling","rate_float":14268.4645},
          "EUR":{"code":"EUR","symbol":"€","rate":"16,634.3953","description":"Euro","rate_float":16634.3953}
     }
}

1
2
3
4
5
6
7
8
9
10
11
12
TextBox1.Text = lCase(TextBox1.Text)
If InStr(TextBox1.Text, "code"":""usd") > 0 Then
  TextBox1.Text = Mid(TextBox1.Text, InStr(TextBox1.Text, "code"":""usd") + 13)
  If InStr(TextBox1.Text, "rate"":") > 0 Then
   TextBox1.Text = Mid(TextBox1.Text, InStr(TextBox1.Text, "rate"":") + 7)
   TextBox1.Text = Mid(TextBox1.Text, 1, InStr(TextBox1.Text, """") - 1)
  Else
   TextBox1.Text = ""
  End If
 Else
  TextBox1.Text = ""
 End If


saludos y felices lineas de programa10n.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar