Android JSON Web服务问题

时间:2012-01-10 06:20:36

标签: android json web-services

我需要在我的Android应用程序中包含JSON, 我已经按照一些教程,让应用程序从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();
}

请注意,从Twitter获取JSON数据时它可以工作:

//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");

2 个答案:

答案 0 :(得分:1)

下面的代码将为您提供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());
        }

答案 1 :(得分:0)

发现了问题,

是因为响应不是数组,是字典,而我正在将响应作为数组,

我不在我的编码计算机中,因此现在无法发布代码,但这基本上就是问题。

谢谢