Reputation: 3149
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");
}
}
}
}
Upvotes: 0
Views: 50
Reputation: 1053
Instead of put(..)
use getString(.)
:
JSONObject jsonObject = new JSONObject(content);
String email = jsonObject.getString("email");
String name = jsonObject.getString("name");
Upvotes: 2