2

i have this Json

{'information':  [{"id_module":"123456","adresse_mac":"0.0.0.1","mot_de_passe":"mdp123"}]}

and i would like to put the "adresse mac" into a string using the url but i really don't know how to do this so if someone can help me it would be awesome.I did lot of research but the tutorials i found are bad.I'am a beginner it's my first app so just be tolerant

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

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyLog()
            .penaltyDialog()
            .build());

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
            .penaltyLog()
            .build());





    JSONObject json = null;
    String str = "";
    HttpResponse response;
    HttpClient myClient = new DefaultHttpClient();
    HttpPost myConnection = new HttpPost("the url i want");
}
    public void parseJson(String jsonString)
    {
        try
        {
            JSONObject json = new JSONObject(jsonString);
            JSONArray userdetails = json.getJSONArray("information");
            for (int i=0; i<userdetails.length(); i++)
            {
                JSONObject user = userdetails.getJSONObject(i);
                String address = user.getString("id_module");
                String name = user.getString("mot_de_passe");
                String userid = user.getString("adresse_mac");


                TextView url = (TextView) findViewById(R.id.url);
                url.setText(userid);



            }
        }
        catch (Exception e) {}

    }
3
  • Can you put your whole code? Commented Dec 20, 2014 at 12:57
  • Just updated right now Commented Dec 20, 2014 at 13:00
  • What is not working in the above code? Commented Dec 20, 2014 at 13:15

2 Answers 2

2

If you are getting this JSON data from the url, I suggest you must use JSONParser class for parsing JSON. There are many methods to parse a JSON string. I personally use JSONParser. Google JSONParser.java and get it downloaded and copy into your source folder. Import JSONparser class into your activity, mostly if I am getting data from a URL using the internet I use Asynctask for parsing from url.

Here is AsyncTask

for calling AsyncTask use new Class_Name().execute();

class Class_Name extends AsyncTask<String, String, String> {


    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // testing on Emulator:
    private String _URL = "Your_URL";


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;
                    try {
            // Building Parameters if parameter present

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("Pram1", param1));

            Log.d("request!", "starting");

            // Posting user data to script
            JSONObject json = jsonParser.makeHttpRequest(_URL, "POST",
                    params);

            // full json response
            Log.d("get data", json.toString());

            int id_module = json.getInt("id_module");
            String adresse_mac = json.getString("adresse_mac");
            String mot_de_passe = json.getString("mot_de_passe");




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

        return null;

    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        }


}

Codelord

Sign up to request clarification or add additional context in comments.

2 Comments

There is no way to do this without GSON ?
You can customize it as you want to. By seeing your code I think you must use AsyncTask for not to crash your application.
1

Try this code

    try {
        JSONObject jsonObject = new JSONObject("");
        JSONArray jsonArray = jsonObject.getJSONArray("information");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject youValue = jsonArray.getJSONObject(i);
            String idModule = youValue.getString("id_module");
            // Use the same for remaining values
        }

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

Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.