0

Basically I have this basic json file.

{
 "email":"[email protected]",
 "name":"Theo Tziomakas"
}

which is located it a server as it looks. Now I want to get it from there back in Android phone.

I use this snippet but I get a null for both email and name.


        JSONObject jsonObject = new JSONObject(content);
        Person p = new Person();
        jsonObject.put("email",p.getEmail());
        jsonObject.put("name",p.getName()); 

I am going to fetch the data from the server using the Volley libray.

  public class MainActivity extends ActionBarActivity {
   public static final String BASE_URL =  
  "http://test.lovenaxos.com/testing.json";
   public List<Person> personList;
   public TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    output = (TextView)findViewById(R.id.text);
    requestData("http://test.lovenaxos.com/testing.json");

}

public void requestData(String url){
    StringRequest request = new StringRequest(url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    personList = JsonParser.parseFeed(response);
                    updateData();
                }
            },
            new Response.ErrorListener(){

                @Override
                public void onErrorResponse(VolleyError error) {



        Toast.makeText(MainActivity.this,error.getMessage(),
        Toast.LENGTH_SHORT).show();
   }
            });
    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);
}

protected void updateData(){
    if(personList!=null){
       for(Person p : personList){
           output.append(p.getName() + "\n");
       }
    }
}

}

3
  • Did you get anything from p.getEmail()?Can you add some more code. Commented May 3, 2015 at 18:39
  • 2
    Maybe I'm misunderstanding your question, but why are you using the put method to extract data from the JSONObject ? Or are you trying to modify the data? Commented May 3, 2015 at 18:44
  • I have more code now so you can see better. Simply I want to fetch that data from the server. Commented May 3, 2015 at 19:02

1 Answer 1

2

Instead of put(..) use getString(.):

JSONObject jsonObject = new JSONObject(content);
String email = jsonObject.getString("email");
String name = jsonObject.getString("name");
Sign up to request clarification or add additional context in comments.

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.