I tried to parse json from a String builder but it give nothings. Here is my code
private class GetClass extends AsyncTask<String, Void, Void> {
private final Context context;
public GetClass(Context c){
this.context = c;
}
protected void onPreExecute(){
progress= new ProgressDialog(this.context);
progress.setMessage("Loading");
progress.show();
}
@Override
protected Void doInBackground(String... params) {
try {
final TextView outputView = (TextView) findViewById(R.id.showOutput);
URL url = new URL("my_url");
HttpURLConnection connection (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
int responseCode = connection.getResponseCode();
//System.out.println("\nSending 'POST' request to URL : " + url);
//System.out.println("Post parameters : " + urlParameters);
//System.out.println("Response Code : " + responseCode);
final StringBuilder output = new StringBuilder();
//output.append(System.getProperty("line.separator") + "Request Parameters " + urlParameters);
//output.append(System.getProperty("line.separator") + "Response Code " + responseCode);
//output.append(System.getProperty("line.separator") + "Type " + "GET");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder responseOutput = new StringBuilder();
//System.out.println("output===============" + br);
while((line = br.readLine()) != null ) {
responseOutput.append(line);
}
br.close();
output.append(responseOutput.toString());
UsersActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject mainObject = new JSONObject(output.toString());
JSONObject id = mainObject.getJSONObject("id");
} catch (JSONException e) {
e.printStackTrace();
}
outputView.setText(output);
progress.dismiss();
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// protected void onPostExecute() {
// progress.dismiss();
// }
}
==============================================
and here is my json data
[{"id":4,"name":"test","email":"[email protected]","approved":1,"admin":0,"responsible":"test","logo":"","president":"test","members":50,"mission":"test","address":"i'm testing the adress","tel":"12345678","website":"http://test.com","facebook":"http://facebook.com/test","created_at":"2016-03-17 11:45:03","updated_at":"2016-03-22 19:47:47"},{"id":5,"name":"test","email":"[email protected]","approved":1,"admin":1,"responsible":"","logo":null,"president":null,"members":null,"mission":null,"address":null,"tel":null,"website":null,"facebook":null,"created_at":"2016-03-19 08:24:50","updated_at":"2016-03-22 19:48:25"},{"id":7,"name":"test","email":"[email protected]","approved":1,"admin":0,"responsible":"","logo":null,"president":null,"members":null,"mission":null,"address":null,"tel":null,"website":null,"facebook":null,"created_at":"2016-03-19 11:30:07","updated_at":"2016-03-22 19:46:10"},{"id":11,"name":"test","email":"[email protected]","approved":1,"admin":0,"responsible":"","logo":null,"president":null,"members":null,"mission":null,"address":null,"tel":null,"website":null,"facebook":null,"created_at":"2016-03-21 15:52:18","updated_at":"2016-03-23 09:57:55"}]