ninjayoto
ninjayoto

Reputation: 703

HttpURLConnection method empty when called

I'm calling a (getData() that should run HttpURLConnection and return a string (later to be casted as Long), the string should be the one line from this URL: https://blockchain.info/tobtc?currency=USD&value=1

Trying to see if it is returning anything , I'm displaying the returned string in the layout.xml file and showing a toast. But both both show as blank

Please do not pay attention to the fact that I'm doing this on the main thread, I'm just trying to make it work first.

What I'm I doing wrong? why it's not returning the string value.

thanks

    package app.com.cryptosudan.android.cryptosudan;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton sdg = (ImageButton) findViewById(R.id.imagesdg);
    ImageButton btc = (ImageButton) findViewById(R.id.imagebtc);

    final TextView display = (TextView) findViewById(R.id.display);
    String price;
    price = (getData());
    display.setText(price);
    Toast.makeText(this, price, Toast.LENGTH_LONG).show();


    sdg.setOnClickListener(sdgpage);
    btc.setOnClickListener(btcpage);

}
//to create an instance of button OnClickListener
View.OnClickListener sdgpage = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, CalculateSdg.class));
    }
};

//to create an instance of button OnClickListener
View.OnClickListener btcpage = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, CalculateBtc.class));
    }
};




public static String getData () {
    BufferedReader reader = null;

    try {
        URL url = new URL("https://blockchain.info/tobtc?currency=USD&value=1");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String line;
        while((line = reader.readLine()) !=null) {
            sb.append (line + "/n");

        }  return sb.toString();

    }catch (Exception e){
        e.printStackTrace();
        return null;
    }finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }


}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

    }

Upvotes: 0

Views: 143

Answers (1)

eurosecom
eurosecom

Reputation: 2992

For http request on main thread change you onCreate method with StrictMode.ThreadPolicy.Builder().permitAll()

I Don't recommend Http request in main thread !!!!

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageButton sdg = (ImageButton) findViewById(R.id.imagesdg);
ImageButton btc = (ImageButton) findViewById(R.id.imagebtc);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

final TextView display = (TextView) findViewById(R.id.display);
String price;
price = (getData());
display.setText(price);
Toast.makeText(this, price, Toast.LENGTH_LONG).show();


sdg.setOnClickListener(sdgpage);
btc.setOnClickListener(btcpage);

}

Upvotes: 1

Related Questions