在json解析中出错了

时间:2013-02-19 11:04:38

标签: android json parsing

请帮我在此代码中找到错误。 我想通过json从远程服务器(php& mysql)获取一些数据然后解析它 问题是结果返回 null ,但它应该在此链接返回名称http://codeincloud.tk/json_android_example.php

package com.shadatv.shada;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class canticlesActivity extends Activity {

    TextView httpStuff;
    HttpClient client;
    JSONObject json;

    final static String URL = "http://codeincloud.tk/json_android_example.php";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canticles);

        httpStuff = (TextView) findViewById(R.id.textView1);
        client = new DefaultHttpClient();
        new Read().execute("name");
    }

    public JSONObject lastTweet() throws ClientProtocolException, IOException, JSONException {
        StringBuilder url = new StringBuilder(URL);

        HttpGet get = new HttpGet(url.toString());
        HttpResponse r = client.execute(get);
        int status = r.getStatusLine().getStatusCode();

        if (status == 200) {
            HttpEntity e = r.getEntity();
            String data = EntityUtils.toString(e);
            JSONArray timeline = new JSONArray(data);
            JSONObject last = timeline.getJSONObject(0);
            return last;

        } else {
            Toast.makeText(getBaseContext(), "error", Toast.LENGTH_SHORT);
            return null;
        }
    }

    public class Read extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {
                json = lastTweet();
                return json.getString(params[0]);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "ahmed .. " + result, Toast.LENGTH_LONG).show();
            httpStuff.setText(result);
        }

    }
}

4 个答案:

答案 0 :(得分:1)

将当前json String解析为:

 HttpEntity e = r.getEntity();
 String data = EntityUtils.toString(e);
 // convert data to json object
 JSONObject last =new JSONObject(data);

因为当前的网络服务仅重新调整JSONObject而不是JSONArray

答案 1 :(得分:1)

您的网址提供以下数据:

  

{“name”:“Froyo”,“version”:“Android 2.2”}

解析JSON的代码块如下:

        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);

这假设您正在阅读的JSON文档是Array而不是Object。 (要么是有效的JSON,要么你提取的文件显然是一个对象)。 Web服务调用可能并不总是返回一个或另一个,因此您必须注意正在解析正确的类型。

答案 2 :(得分:0)

复制并过去现在它正在工作。 我更改了JSONObject last = new JSONObject(data);

因为http://codeincloud.tk/json_android_example.php重新调整JSONObject

package com.shadatv.shada;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class canticlesActivity extends Activity {

    TextView httpStuff;
    HttpClient client;
    JSONObject json;

    final static String URL = "http://codeincloud.tk/json_android_example.php";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.canticles);

        httpStuff = (TextView) findViewById(R.id.textView1);
        client = new DefaultHttpClient();
        new Read().execute("name");
    }

    public JSONObject lastTweet() throws ClientProtocolException, IOException, JSONException {
        StringBuilder url = new StringBuilder(URL);

        HttpGet get = new HttpGet(url.toString());
        HttpResponse r = client.execute(get);
        int status = r.getStatusLine().getStatusCode();

        if (status == 200) {
            HttpEntity e = r.getEntity();
            String data = EntityUtils.toString(e);

            JSONObject last =  new JSONObject(data);
            return last;

        } else {
            Toast.makeText(getBaseContext(), "error", Toast.LENGTH_SHORT);
            return null;
        }
    }

    public class Read extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {
                json = lastTweet();
                return json.getString(params[0]);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "ahmed .. " + result, Toast.LENGTH_LONG).show();
            httpStuff.setText(result);
        }

    }
}

答案 3 :(得分:0)

最好使用GSON Library,这比手动解析要简单得多。

图书馆的链接是:http://code.google.com/p/google-gson/