Reputation: 557
I've been trying to figure this out for the past 4 days and I am about to pull my hair out!
I need your help, can anyone tell me what im doing wrong?
Here is the JSON format link I am using for development: REMOVED FOR PRIVACY CONCERNS
Here is my code:
public class JSONActivity extends Activity {
TextView http;
HttpClient client;
JSONObject json;
final static String URL = "REMOVED FOR PRIVACY CONCERNS
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
http = (TextView) findViewById(R.id.http);
client = new DefaultHttpClient();
new Read().execute("firstName");
}
public JSONObject getpw(String password)
throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(password);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray getname = new JSONArray(data);
JSONObject last = getname.getJSONObject(3);
return last;
} else {
Toast.makeText(JSONActivity.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json=getpw("trustme");
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
http.setText(result);
}
}}
Any help would be greatly appreciated.
Upvotes: 0
Views: 286
Reputation: 191
You are expecting a JSONArray back, but the webservice is just returning a JSONObject.
I made the following changes:
JSONArray getname = new JSONArray(data);
--> JSONObject getname = new JSONObject(data);
and in the Async Task, I made the following change:
return json.getString("firstName");
Here's the complete code:
public class PlaygroundActivity extends Activity {
TextView http;
HttpClient client;
JSONObject json;
final static String URL = "REMOVED FOR PRIVACY CONCERNS
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
http = (TextView) findViewById(R.id.http);
client = new DefaultHttpClient();
new Read().execute("firstName");
}
public JSONObject getpw(String password) throws ClientProtocolException,
IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(password);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject getname = new JSONObject(data);
return getname;
} else {
Toast.makeText(PlaygroundActivity.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = getpw("trustme");
return json.getString("firstName");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
http.setText(result);
}
}
}
Upvotes: 2
Reputation: 19310
You can use following code to get response from server and then parse json.
public static void extractParameters(){
String urlRequest = "REMOVED FOR PRIVACY CONCERNS";
String response = getServerResponse(urlRequest);
try {
JSONObject json = new JSONObject(response);
boolean suspended = json.getBoolean("suspended");
String firstName = json.getString("firstName");
String lastName = json.getString("lastName");
int checkdin = json.getInt("checkedin");
int checkindatetime = json.getInt("checkindatetime");
//Get Json object address
JSONObject address = json.getJSONObject("address");
String streedAddress = address.getString("streetAddress");//In same way get city etc
//Get PhoneNumber Array
JSONArray phoneNumbers = json.getJSONArray("phoneNumber");
String type = phoneNumbers.getJSONObject(0).getString("type");
String number = phoneNumbers.getJSONObject(0).getString("number");// and so on for 1,2,3...
} catch (Exception e) {
// TODO: handle exception
}
}
These methods will help to get response from server
/**
*
* @param urlRequest URL of server
* @return
*/
public static String getServerResponse(String urlRequest){
Log.d("urlRequest",urlRequest);
String response = "";
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(urlRequest).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
response = read(conn.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
Log.d("response",response);
return response.trim();
}
private static String read(InputStream in) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
in.close();
return sb.toString();
}
Example of JSON Response you sent is:
{
"suspended": "false",
"firstName": "John",
"lastName": "Smith",
"checkedin": 0,
"checkindatetime": 0,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 111-1111"
},
{
"type": "fax",
"number": "646 222-2222"
}
]
}
Upvotes: 0