我使用BitNami来测试我的django项目。我正在从android创建一个连接,向它传递一个url来从数据库中获取一些数据。数据采用以下形式:
{"apps": ["False", "Hello from notepade++", "My App", "Test"]}
这里是我如何在android
中获取和接收数据public static ArrayList<String> getApps(){
HttpClient httpclient = new DefaultHttpClient();
HttpResponse responseCategories;
int categoriesStatusCode =0;
String responseCategoriesString = "";
ArrayList<String> out = new ArrayList<String>();
HttpGet httpgetCategories = new HttpGet("http://safwany/sampleproject/applications/fetch_apps");
try {
responseCategories = httpclient.execute(httpgetCategories);
categoriesStatusCode = responseCategories.getStatusLine().getStatusCode();
try {
responseCategoriesString = EntityUtils.toString(responseCategories.getEntity());
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
if(categoriesStatusCode==200){
JSONObject object;
try {
System.out.println(responseCategoriesString);
object = new JSONObject(responseCategoriesString);
JSONArray Jarray = object.getJSONArray("apps");
for(int i = 0; i < Jarray.length(); i++)
{
String s = (String) Jarray.get(i);
out.add(s);
//categoriesMap.put(s, 0);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return out;
}
当我尝试打印responseCategoriesString时,我得到以下内容:
04-01 22:55:56.335: I/System.out(9325): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta http-equiv="refresh" content="0;url=http://search.tedata.net/main?InterceptSource=0&ClientLocation=eg&ParticipantID=esrkzqp9pwh62lmydaf4wsy3i92taus2&FailureMode=1&SearchQuery=&FailedURI=http%3A%2F%2Fsafwany%2Fsampleproject%2Fapplications%2Ffetch_apps&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0"/><script type="text/javascript">url="http://search.tedata.net/main?InterceptSource=0&ClientLocation=eg&ParticipantID=esrkzqp9pwh62lmydaf4wsy3i92taus2&FailureMode=1&SearchQuery=&FailedURI=http%3A%2F%2Fsafwany%2Fsampleproject%2Fapplications%2Ffetch_apps&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0";if(top.location!=location){var w=window,d=document,e=d.documentElement,b=d.body,x=w.innerWidth||e.clientWidth||b.clientWidth,y=w.innerHeight||e.clientHeight||b.clientHeight;url+="&w="+x+"&h="+y;}window.location.replace(url);</script></head><body></body></html>
04-01 22:55:56.335: W/System.err(9325): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
答案 0 :(得分:0)
当我查看您的响应数据时,我认为您遇到了某些防火墙或类似的问题:
http://search.tedata.net/main?InterceptSource=0&ClientLocation=eg&ParticipantID=esrkzqp9pwh62lmydaf4wsy3i92taus2&FailureMode=1&SearchQuery=&FailedURI=http%3A%2F%2Fsafwany%2Fsampleproject%2Fapplications%2Ffetch_apps&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0
包含您请求的网址:
FailedURI=http%3A%2F%2Fsafwany%2Fsampleproject%2Fapplications%2Ffetch_apps
但FailedURI
听起来像公司的火灾/网关带过滤器...你应该检查你的模拟器/设备是否真的可以访问safwany
“域”。也许您应该尝试IP而不是safwany
?