Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Retieving Values using HttpURLConnection - Help

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Retieving Values using HttpURLConnection - Help

    I found this code to retieve a vaule using HttpURLConnection request,

    This is the URL I use (minus the api key)
    http://emoncms.org/feed/value.json?a...pikey&id=52931

    Below is the code i found, i added 2 new variables to collect a 2nd value, but i can't work out how to add a 2nd request to the code, here is the 2nd url:
    http://emoncms.org/feed/value.json?a...pikey&id=52932

    Thanks for any help you can give....

    Code:

    *******************************************

    package com.example.helloworld;

    import java.io.BufferedInputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    // Time in ms in which to fetch a feed value update
    private int updateInterval = 5000;
    // Handler for periodic updates
    private Handler periodicHandler = new Handler();
    // Handler for updating UI on data update
    private Handler uiUpdateHandler = new Handler();

    private TextView powerval;
    private String tpowerused;
    private TextView solarval;
    private String tsolar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Log.i("EmonLog", "onCreate");

    powerval = (TextView) findViewById(R.id.powervalue);
    }

    protected void onPause()
    {
    super.onPause();
    Log.i("EmonLog", "onPause");
    Log.i("EmonLog", "Stopping periodic updater");
    stopRepeatingTask();
    }

    protected void onResume()
    {
    super.onResume();
    Log.i("EmonLog", "onResume");
    Log.i("EmonLog", "Starting periodic updater");
    startRepeatingTask();
    }

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

    Runnable mStatusChecker = new Runnable() {

    @Override
    public void run() {
    Log.i("EmonLog", "Periodic");

    try {
    String result = new HTTP().execute("http://emoncms.org/feed/value.json?apikey=myapikey&id=52931").get();
    result = result.replaceAll("\"","");

    tpowerused = result;
    Log.i("EmonLog", "Periodic "+tpowerused);

    uiUpdateHandler.post(new Runnable(){
    public void run() {
    powerval.setText(tpowerused+"W");
    }});



    } catch (Exception e) {

    }

    periodicHandler.postDelayed(mStatusChecker, updateInterval);
    }
    };

    void startRepeatingTask() {
    mStatusChecker.run();
    }

    void stopRepeatingTask() {
    periodicHandler.removeCallbacks(mStatusChecker);
    }
    }

    class HTTP extends AsyncTask<String, Void, String>
    {
    @Override
    protected String doInBackground(String... params) {
    String result = "";
    try {
    String urlstring = params[0];
    Log.i("EmonLog", "HTTP Connecting: "+urlstring);
    URL url = new URL(urlstring);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    try {
    InputStream reader = new BufferedInputStream(urlConnection.getInputStream() );

    String text = "";
    int i = 0;
    while((i=reader.read())!=-1)
    {
    text += (char)i;
    }
    Log.i("EmonLog", "HTTP Response: "+text);
    result = text;

    } catch (Exception e) {
    Log.i("EmonLog", "HTTP Exception: "+e);
    }
    finally {
    Log.i("EmonLog", "HTTP Disconnecting");
    urlConnection.disconnect();
    }

    } catch (Exception e) {
    e.printStackTrace();
    Log.i("EmonLog", "HTTP Exception: "+e);
    }

    return result;
    }
    }

    *******************************************


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Retieving Values using HttpURLConnection - Help

    Thread moved.

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

Similar Threads

  1. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  2. Replies: 2
    Last Post: October 26th, 2013, 09:56 AM
  3. Java HTTPURLConnection
    By mike2033 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: May 1st, 2013, 04:33 PM
  4. HttpURLConnection doesnt quite cut it
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 23rd, 2012, 08:48 AM
  5. Passing session ID over HttpURLConnection
    By zuzacat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 20th, 2011, 09:18 AM