Reputation: 16138
I need to include JSON in my android app, I have followed some tutorials and have the app reading a test from twitter
package com.or.jsonswitch;
import ***
public class JsonTestMySwitchActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("msg", "app started 1");
String readResponse = readResponse(); //crea un string llenado por lo que devuelve la funcion()
try {
JSONArray jsonArray = new JSONArray(readResponse);
Log.d("msg", "number of entries::"+jsonArray.length());
Log.d("msg", "response::"+jsonArray);
} catch (Exception e) {
e.printStackTrace(); //donde va el stacktrace?
}
}
public String readResponse() { //autogenera como private!, cambio a public!
Log.d("msg" , "entra a buscar");
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
//HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json");
HttpGet httpGet = new HttpGet("http://myswitch.merged.stage.orchard.net.au/LifftService.svc/JSON/CoverageSearchByLatLong/-33.881393,151.214534");
//httpGet.setHeader("Content-Type", "text/plain; charset=utf-8");
//httpGet.setHeader("Content-Type", "json");
try {
HttpResponse response = client.execute(httpGet); //que es?
StatusLine statusLine = response.getStatusLine(); //que es?
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { //200?
HttpEntity entity = response.getEntity(); //que es?
InputStream content = entity.getContent(); //que es?
BufferedReader reader = new BufferedReader(new InputStreamReader(content)); //que es?
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.d("msg" , "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.d("msg", "client protocol exception:"+e);
} catch (IOException e) {
e.printStackTrace();
Log.d("msg", "client protocol exception:"+e);
}
return builder.toString();
}
Note that it works when fetching the JSON data from Twitter:
//HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json");
but appears blank when fetching from my desired server:
HttpGet httpGet = new HttpGet("http://myswitch.merged.stage.orchard.net.au/LifftService.svc/JSON/CoverageSearchByLatLong/-33.881393,151.214534");
I have no error messages when it returns blank,
do I have to set headers specifying it is JSON? How?
//httpGet.setHeader("Content-Type", "json");
Upvotes: 0
Views: 1061
Reputation: 1495
Below code will give you JSONObject then loop take jsonarray from this jsonobject and loop through the results...Hope it will help you
JSONObject jobject = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myswitch.merged.stage.orchard.net.au/LifftService.svc/JSON/CoverageSearchByLatLong/-33.881393,151.214534");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jobject = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
Upvotes: 1
Reputation: 16138
found the problem,
is because the response is not an array, is a dictionary, and I was making the response an array,
im not in my coding computer so cant post code right now, but that was basically the problem.
thanks
Upvotes: 0