2017-05-17 60 views
0

我retriving從PHP數據retriving myjson串時使用json具有的PHP的所有值時,我使用JSON數組爲空,而具有的JSONObject

array= j.getJSONArray("result"); 

j具有所述第一數組元素到Android值但我的數組爲空。 PLZ有人說我這背後

public void getData() { 
     class GetDataJSON extends AsyncTask<String, Void, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
       HttpPost httppost = new HttpPost("http://futuroitsolutions.in/php_android_api/data1.php"); 

       // Depends on your web service 
       httppost.setHeader("Content-type", "application/json"); 

       InputStream inputStream = null; 
       String result = null; 
       try { 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 

        inputStream = entity.getContent(); 
        // json is UTF-8 by default 
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (Exception e) { 
        // Oops 
       } finally { 
        try { 
         if (inputStream != null) inputStream.close(); 
        } catch (Exception squish) { 
        } 
       } 
       return result; 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       myJSON = result; 
       ShowList(); 
      } 
     } 
     GetDataJSON g = new GetDataJSON(); 
     g.execute(); 

    } 
public void ShowList() { 
try { 

    String vi=myJSON; 
    vi=vi.replace('[',' ').replace(']',' '); 
     JSONObject j = new JSONObject(vi); 
     array= j.getJSONArray("result"); 
     try { 


     for(int i=0;i<array.length();i++){ 
      JSONObject c = array.getJSONObject(i); 
      id = c.getString(TAG_ID); 
      name = c.getString(TAG_NAME); 
      km = c.getString(TAG_KILOMETER); 
      time = c.getString(TAG_TIMING); 
      // get your data from jsonobject 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     // String regno = c.getString(TAG_REG); 
     // String phone = c.getString(TAG_PHONE); 
     // String category = c.getString(TAG_CAT); 

    } 

     catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    TableLayout tv = (TableLayout) findViewById(R.id.table); 
     tv.removeAllViewsInLayout(); 
     int flag = 1; 
    int c=array.length(); 
     for (int i = 0; i < c ; i++) { 
     TableRow tr = new TableRow(MainActivity.this); 
     tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     if (flag == 1) { 
      TextView b6 = new TextView(MainActivity.this); 
      b6.setText("Id"); 
      b6.setTextColor(Color.BLUE); 
      b6.setTextSize(15); 
      tr.addView(b6); 
      TextView b19 = new TextView(MainActivity.this); 
      b19.setPadding(10, 0, 0, 0); 
      b19.setTextSize(15); 
      b19.setText("Task name"); 
      b19.setTextColor(Color.BLUE); 
      tr.addView(b19); 
      TextView b29 = new TextView(MainActivity.this); 
      b29.setPadding(10, 0, 0, 0); 
      b29.setText("Timing"); 
      b29.setTextColor(Color.BLUE); 
      b29.setTextSize(15); 
      tr.addView(b29); 
      tv.addView(tr); 
      TextView b21 = new TextView(MainActivity.this); 
      b29.setPadding(10, 0, 0, 0); 
      b29.setText("Kilometers"); 
      b29.setTextColor(Color.BLUE); 
      b29.setTextSize(15); 
      tr.addView(b21); 
      tv.addView(tr); 

      final View vline = new View(MainActivity.this); 
      vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2)); 
      vline.setBackgroundColor(Color.BLUE); 
      tv.addView(vline); 
      flag = 0; 
     } else { 
      //JSONObject json_data = jArray.getJSONObject(i); 
      // Log.i("log_tag", "id: " + json_data.getInt("Id") + ", Username: " + json_data.getString("Task name") + ", No: " + json_data.getString("Kilometers")); 
      TextView b = new TextView(MainActivity.this); 
      String stime =id; 
      b.setText(stime); 
      b.setTextColor(Color.RED); 
      b.setTextSize(15); 
      tr.addView(b); 
      TextView b1 = new TextView(MainActivity.this); 
      b1.setPadding(10, 0, 0, 0); 
      b1.setTextSize(15); 
      String stime1 = name; 
      b1.setText(stime1); 
      b1.setTextColor(Color.BLACK); 
      tr.addView(b1); 
      TextView b2 = new TextView(MainActivity.this); 
      b2.setPadding(10, 0, 0, 0); 
      String stime2 = time; 
      b2.setText(stime2); 
      b2.setTextColor(Color.BLACK); 
      b2.setTextSize(15); 
      tr.addView(b2); 
      tv.addView(tr); 
      TextView b3 = new TextView(MainActivity.this); 
      b2.setPadding(10, 0, 0, 0); 
      String stime3 = km; 
      b2.setText(stime3); 
      b2.setTextColor(Color.BLACK); 
      b2.setTextSize(15); 
      tr.addView(b2); 
      tv.addView(tr); 
      final View vline1 = new View(MainActivity.this); 
      vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
      vline1.setBackgroundColor(Color.WHITE); 
      tv.addView(vline1); 
     } 
    } 
} 

}

當我調試,我得到

vi= 
{"id":"1","Taskname":"edapally to kakkanad","SourceLa":"10.0236761","SourceLo":"76.311623","Path1La":"10.0327153","Path1Lo":"76.331889","Path2La":"10.0231412","Path2Lo":"76.3409954","DestinationLa":"10.0158605","DestinationLo":"76.3418666","Timing":"10 a.m","Kilometer":"8"} 

j={"id":"1","Taskname":"edapally to kakkanad","SourceLa":"10.0236761","SourceLo":"76.311623","Path1La":"10.0327153","Path1Lo":"76.331889","Path2La":"10.0231412","Path2Lo":"76.3409954","DestinationLa":"10.0158605","DestinationLo":"76.3418666","Timing":"10 a.m","Kilometer":"8"} 

陣列的確切原因= NULL 的json

[{」 id「:」1「,」Taskname「:」edapally to kakkanad「,」SourceLa「:」10.0236761「,」SourceLo「:」76.311623「,」Path1La「:」10.032715 3「,」Path1Lo「:」76.331889「,」Path2La「:」10.0231412「,」Path2Lo「:」76.3409954「,」DestinationLa「:」10.0158605「,」DestinationLo「:」76.3418666「,」Timing「 」, 「一公里」: 「8」}]

Data1.php

<?php 
    include('sql.php'); 

    mysql_query('SET CHARACTER SET utf8') ; 

     $result = mysql_query('SELECT * FROM Location') or    die(mysql_error()); 

     while($row = mysql_fetch_assoc($result)) 
     { 

     $rows[] = $row; 

    } 

     $final=array("result"=>$rows); 

     echo json_encode($rows); 
    ?> 
+0

你得到的JSONObject不JSONArray在vi如你所說。 –

+0

如果你想要一個'result',你應該在php中編碼$ final,而不是$ rows。 – YvesLeBorg

+0

雅我只編碼最後,但它也給了數組null,所以只有我編碼$行,還當我編碼我得到像JSON {「結果」............ [...... ......]} –

回答

0

您還沒有while循環外部定義的變量$行。這就是爲什麼$行的範圍是未定義的。

<?php 
include('sql.php'); 
mysql_query('SET CHARACTER SET utf8') ; 
$rows=array(); 
$result = mysql_query('SELECT * FROM Location') or die(mysql_error()); 
while($row = mysql_fetch_assoc($result)) 
{ 
    $rows[] = $row; 
} 
$final=array("result"=>$rows); 
echo json_encode($rows); 
0

試試這個。

String vi=myJSON; 
JSONArray array = new JSONArray(vi); 
+0

我已經試過這個,但同樣的數組= null –

+0

給我你的json對象 – Ashish

+0

你還在做這個嗎? vi = vi.replace('[','').replace(']','');如果是,那麼請刪除這一行 – Ashish

1

你可以試試下面的代碼:

JSONArray jsonArray = new JSONArray(vi); 
        for(int k = 0; k < jsonArray.length(); k++) 
        { 
         JSONObject jsonObject = jsonArray.getJSONObject(k); 

         jsonObject.getString("id"); 
         jsonObject.getString("Taskname"); 

         ............. 
         // parse all fields like above 
        } 
+0

我現在正在獲取數組,但其運算似乎爲空 –

+1

首先檢查Postman中的Web服務輸出。 –