2012-08-14 107 views
2

我得到這個logcat的:無法從android中獲取Json的php?

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


?> 
+1

郵服務器代碼。 – 2012-08-14 08:59:18

+0

我使用JsonLint驗證了它,發現很好。 – 2012-08-14 09:00:56

+0

正如boris所說,發佈生成的Json,你使用什麼編碼類型來生成json數據? – Amyth 2012-08-14 09:01:53

回答

2

因爲你得到的是不JSONArray - 它是JSONObject

JSONArray你的情況看起來像:

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

這是一個元素(這是JSONObject,其中包含字段)的數組。

我不知道PHP的那麼多(我不知道如何json_encode作品),但試試這個:用於生成JSON,以及產生的JSON

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

,我們將如何實現? – 2012-08-14 09:13:07

+0

@AyushGoyal我編輯了我的答案 – 2012-08-14 09:13:58

+0

非常感謝!它的工作\ m / – 2012-08-14 09:15:07