【问题标题】:Not able to get Json from php in android?无法从android中的php获取Json?
【发布时间】:2012-08-10 13:00:14
【问题描述】:

我得到了这个 Logcat:

08-14 11:42:55.923: W/System.err(1137): org.json.JSONException: Value {"error":0,"success":0,"tag":"helloworld"} of type org.json.JSONObject cannot be converted to JSONArray

我该如何解决这个问题?

08-14 11:42:55.923: W/System.err(1137):     at org.json.JSON.typeMismatch(JSON.java:111)

我使用的代码是:

public class MainActivity extends Activity {

    TextView tv;
    HttpClient client;
    JSONObject json;
final static String URL = "http://192.168.1.9/sumit/hello.php";
    //final static String URL = "http://json.org/example.html";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv1);
        client = new DefaultHttpClient();

        Log.e("my","check");
        new Read().execute("tag");
//      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
//              .detectAll().penaltyLog().build();
//      StrictMode.setThreadPolicy(policy);

    }

    public JSONObject myData() 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 datastream = new JSONArray(data);
            JSONObject message = datastream.getJSONObject(0);
            return message;
        } else {
            Toast.makeText(MainActivity.this, "error encountered",
                    Toast.LENGTH_SHORT).show();
            return null;
        }

    }

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

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                json = myData();

                                    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
            tv.setText(result);

        }

    }

}

生成的 JSON 代码:

{"tag":"hello","success":0,"error":0}

用于生成的代码:

<?php


    // response Array
    $response = array("tag" => helloworld, "success" => 0, "error" => 0);


   echo json_encode($response);


?>

【问题讨论】:

  • 贴出用于生成json的服务器代码以及生成的json。
  • 我使用 JsonLint 对其进行了验证,结果很好。
  • 正如鲍里斯所说,发布生成的Json,你使用什么编码类型来生成json数据?

标签: php android json


【解决方案1】:

因为你得到的不是JSONArray——而是JSONObject

JSONArray 在您的情况下将如下所示:

[{"error":0,"success":0,"tag":"helloworld"}]

这是一个包含一个元素的数组(即JSONObject,其中包含字段)。

我不太了解 php(而且我不知道 json_encode 是如何工作的),但试试这个:

<?
    $response = array(array("tag" => helloworld, "success" => 0, "error" => 0));
    echo json_encode($response);
?>

【讨论】:

  • 我们将如何做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
相关资源
最近更新 更多