首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json语法分析错误

json语法分析错误
EN

Stack Overflow用户
提问于 2013-02-19 11:04:37
回答 3查看 832关注 0票数 0

请帮我找出这段代码中的错误。我想通过json从远程服务器(php & mysql)获取一些数据,然后解析它,问题是结果返回,但是它应该在这个链接:example.php处返回名称

代码语言:javascript
复制
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);
        }

    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-19 11:11:43

复制和过去,现在,它正在工作。我更改了JSONObject last =新的JSONObject(data);

因为example.php重新调整了一个JSONObject

代码语言:javascript
复制
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);
        }

    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-02-19 11:08:50

将当前的json字符串解析为:

代码语言:javascript
复制
 HttpEntity e = r.getEntity();
 String data = EntityUtils.toString(e);
 // convert data to json object
 JSONObject last =new JSONObject(data);

因为当前的webservice仅重新运行一个JSONObject而不是JSONArray

票数 1
EN

Stack Overflow用户

发布于 2013-02-19 11:21:12

更好的方法是使用,这比手工解析要简单得多。

库的链接是:http://code.google.com/p/google-gson/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14955883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档