Reputation: 29
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) {}
}
Upvotes: 2
Views: 15207
Reputation: 1120
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
}
}
Upvotes: 2
Reputation: 10069
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
Upvotes: 1