Home android Android How to make Get Request

Android How to make Get Request [Duplicate]

Author

Date

Category

To me when you click on the button in the MainActivity you need to request data
There is a special address on which they lie in JSON view

It seems to me either in Java as difficult?)

I specified the rights for the Internet.
All, with the code of the problem)

url url = new url ("http://hashcode.ru") .OpenStream ()

Here emphasizes the red line with the address …. (((


Answer 1, Authority 100%

Public StriC String Doget (String URL)
      Thrown Exception {
    URL OBJ = NEW URL (URL);
    HttpurlConnection Connection = (httpurlconnection) obj.openconnection ();
    // Add Reuqest Header
    Connection.setRequestmethod ("Get");
    Connection.SetRequestProperty ("User-Agent", "Mozilla / 5.0");
    Connection.SetRequestProperty ("Accept-Language", "EN-US, EN; Q = 0.5");
    Connection.SetRequestProperty ("Content-Type", "Application / JSON");
    BufferedReader BufferedReader = New BufferedReader (Connection.getInputStream ()));
    String InputLine;
    StringBuffer Response = New StringBuffer ();
    While ((InputLine = bufferedreader.ReadLine ())! = NULL) {
      Response.APPEND (Inputline);
    }
    bufferedreader.close ();
// PRINT RESULT.
    Log.d (Tag, "Response String:" + response.tostring ());
    Return Response.Tostring ();
  }

You can use this:

new asynctask & lt; void, string, string & gt; () {
    @Override
    PROTECTED STRING DOINBACKGRUND (Void ... Voids) {
      String S = "";
      Try {
        s = doget (myurl);
      } Catch (Exception E) {
        E.PrintStackTrace ();
      }
      Return S;
    }
    @Override
    PROTECTED VOID OnPostexecute (Final String Result) {
      Runonuithread (New Runnable () {
        @Override
        Public void Run () {
          tvrez.settext (Result);
        }
      });
    }
  } .execute ();

Answer 2, Authority 25%

did with the stream:

Public Class JSontask Extends Asynctask & Lt; String, String, String & GT;
{
  Private Final String URL = "Google.com";
  Private ARRAYLIST & LT; NameValuepair & GT; postparams = new arraylist & lt; & gt; ();
  Private JSONObject jsonobject = new jsonobject ();
  @Override
  PROTECTED VOID ONPREEXECUTE ()
  {
    super.onpreexecute ();
  }
  @Override
  Protected String DoinBackground (String ... Params)
  {
      postparams.add ("Paramkey", "Paramvalue"));
      jsonobject = jsonparser.makehttprequest (url, "get", postparams);
      Return jsonobject.tostring ();
  }
}

And here is the parser to parsing JSON:

Public Class JSONPARSER {
  static inputstream is = null;
  static jsonobject jobj = null;
  static string json = "";
    Public Static JSONObject MakeHttpRequest (String URL, String Method, List & LT; NameValuepair & GT; Params) {
      Try {
        if (Method == "POST") {
          DefaultHttpclient httpclient = new defaulthttpclient ();
          Httppost httppost = new httppost (url);
          httppost.setentity (New UrlencodedForMentity (Params));
          Httpresponse httpresponse = httpclient.execute (httppost);
          Httpentity httpentity = httpresponse.getentity ();
          is = httpentity.getContent ();
        } ELSE If (Method == "Get") {
          DefaultHttpclient httpclient = new defaulthttpclient ();
          String ParamString = Urlencodedutils.Format (Params, "UTF-8");
          url + = "?" + ParamString;
          Httpget httpget = new httpget (URL); 
Httpresponse httpresponse = httpclient.execute (httpget);
          Httpentity httpentity = httpresponse.getentity ();
          is = httpentity.getContent ();
        }
      } Catch (UnsupportedEncodingException E)
      {
        E.PrintStackTrace ();
      } Catch (ClientProtocolexception E) {
        E.PrintStackTrace ();
      } Catch (IoException E)
      {
        System.out.PrintLN ("Internet Error")
      }
      Try {
        BufferedReader Reader = New BufferedReader (New InputStreamReader (IS, ISO-8859-1), 8);
        STRINGBUILDER SB = new stringbuilder ();
        String Line = NULL;
        While ((Line = Reader.ReadLine ())! = NULL) {
          SB.APPEND (LINE + "\ N");
        }
        is.close ();
        json = sb.tostring ();
      } Catch (Exception E) {
        System.Out.printLN ("Buffer Error: Error Converting Result" + E.Tostring ());
      }
      Try {
        JOBJ = NEW JSONOBJECT (JSON);
      } Catch (jsonexception e) {
        System.Out.printLN ("JSON PARSER: + ERROR PARSING DATA" + E.TOSTRING ());
      }
      RETURN JOBJ;
    }
  }
Previous article504 Gateway Time-Out
Next articleWrite on PHP CLI

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions