1

我正在使用 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
4

1 回答 1

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?

于 2013-04-01T21:14:55.107 回答