Android的JSON Web服务问题
问题描述:
我需要包括JSON在我的Android应用程序, 我按照一些教程,并安装了该应用从Twitter读取测试Android的JSON Web服务问题
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();
}
注意,它取出由JSON数据时的工作原理Twitter的:
//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");
当它返回空白时,我没有错误消息,
我必须设置指定它的标头是JSON吗?怎么样?
//httpGet.setHeader("Content-Type", "json");
答
发现问题,
是因为响应不是一个数组,是一个字典,我正在作出响应一个数组,
即时通讯不在我的编码计算机,所以现在不能邮编代码,但那是基本上是问题。
谢谢
答
下面的代码会给你的JSONObject然后循环从这个的JSONObject和遍历结果采取jsonarray ...希望它会帮助你
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());
}
我会开始通过对URL进行cURL调试来查看Android响应和cURL响应之间的差异。 – curioustechizen 2012-01-10 06:26:59