Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get json data from php server to android mobile

Tags:

json

android

php

I am having an application in which I want to get json data from a php web server to android mobile. What I am having is a URL and hitting that URL gives me the json data like
{"items":[{"latitude":"420","longitude":"421"}]}. But I want to retrieve this json format in my android mobile and get the values of latitude and longitude from json format.

How can we get that on android mobile..?

Thanks in advance..

like image 372
Jagdeep Singh Avatar asked Jul 20 '12 12:07

Jagdeep Singh


People also ask

How do I extract data from JSON with PHP?

Use json_decode() Function to Extract Data From JSON in PHP. We will use the built-in function json_decode() to extract data from JSON. We will convert the JSON string to an object or an array to extract the data. The correct syntax to use this function is as follows.

Can JSON be used in mobile application?

The Android application is simple. It contains full copies of the XML and JSON data feeds and gives the user the option of parsing either one.

Can PHP send JSON?

The JSON data format is mostly used in web services to interchange data through API. When you working with web services and APIs, sending JSON data via POST request is the most required functionality. PHP cURL makes it easy to POST JSON data to URL.


1 Answers

First do URL Connection

String parsedString = "";

    try {

        URL url = new URL(yourURL);
        URLConnection conn = url.openConnection();

        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        InputStream is = httpConn.getInputStream();
        parsedString = convertinputStreamToString(is);

    } catch (Exception e) {
        e.printStackTrace();
    }

JSON String

{
"result": "success",
"countryCodeList":
[
  {"countryCode":"00","countryName":"World Wide"},
  {"countryCode":"kr","countryName":"Korea"}
] 
}

Here below I am fetching country details

JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);

JSONArray valArray1 = valArray.getJSONArray(1);

valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");

int len = valArray1.length();

for (int i = 0; i < valArray1.length(); i++) {

 Country country = new Country();
 JSONObject arr = valArray1.getJSONObject(i);
 country.setCountryCode(arr.getString("countryCode"));                        
 country.setCountryName(arr.getString("countryName"));
 arrCountries.add(country);
}




public static String convertinputStreamToString(InputStream ists)
        throws IOException {
    if (ists != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader r1 = new BufferedReader(new InputStreamReader(
                    ists, "UTF-8"));
            while ((line = r1.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } finally {
            ists.close();
        }
        return sb.toString();
    } else {
        return "";
    }
}
like image 65
Nirali Avatar answered Sep 24 '22 00:09

Nirali