Get IP in JSON

Posted on Posted in Android

http://www.rameshchavan.in/myip.php

 

Android Code:

final TextView mTextView = (TextView) findViewById(R.id.text);
...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.rameshchavan.in/myip.php";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display IP address.
        mTextView.setText("Response is: "+ response );
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

iOS Code (Swift 2.0 +):

 let urlPath: String = "http://www.rameshchavan.in/myip.php"
 let url: NSURL = NSURL(string: urlPath)!
 let request1: NSURLRequest = NSURLRequest(URL: url)
 let queue:NSOperationQueue = NSOperationQueue()

 NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in

 do {
 if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
 // Display IP address.

print("ASynchronous\(jsonResult)")
 }
 } catch let error as NSError {
 print(error.localizedDescription)
 }


 })

Leave a Reply

Your email address will not be published. Required fields are marked *