2016-09-19 89 views
1

我試圖從服務器上的數據庫中讀取一些數據,但我有此錯誤:Android的凌空錯誤字符串不能轉換爲JSONObject的

09-19 21:14:55.294 8376-8376/com.example.user.firebasenot I/System.out: Try2com.android.volley.ParseError: org.json.JSONException: Value connected of type java.lang.String cannot be converted to JSONObject 

我想嘗試,如果服務器上的代碼工作使用先進的REST客戶端和郵差也和它的工作非常好,這是結果:

{ 
    "students": [ 
    { 
     "id": "1", 
     "firstname": "saleh", 
     "lastname": "refai", 
     "age": "333" 
    }, 
    { 
     "id": "2", 
     "firstname": "ali", 
     "lastname": "hariri", 
     "age": "22" 
    } 
    ] 
} 

,但是當我試圖在Android應用程式我以前的錯誤響應 這裏是我的代碼:

public class Insertion extends AppCompatActivity { 

    String token1; 
    EditText firstname,lastname,age; 
    Button register,show; 
    TextView result; 
    RequestQueue requestQueue; 
    String inserturl="http://saleh923.freeoda.com/insertstudent.php"; 
    String showurl="http://saleh923.freeoda.com/showstudent.php"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nsertion); 
     Bundle b=getIntent().getExtras(); 
//  token1=b.getString("token"); 

     firstname=(EditText)findViewById(R.id.editText); 
     lastname=(EditText)findViewById(R.id.editText2); 
     age=(EditText)findViewById(R.id.editText3); 
     register=(Button) findViewById(R.id.button2); 
     show=(Button) findViewById(R.id.button3); 
     result=(TextView) findViewById(R.id.textView2); 

     requestQueue= Volley.newRequestQueue(getApplicationContext()); 
     show.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, showurl, new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         try { 
          System.out.println("Try1"+response); 
          JSONArray students=response.getJSONArray("students"); 
          for(int i=0;i<students.length();i++) 
          { 
           JSONObject student=students.getJSONObject(i); 
           String firstname=student.getString("firstname"); 
           String lastname=student.getString("lastname"); 
           String age=student.getString("age"); 
           result.append(firstname+" "+lastname+" "+ age+ " \n"); 

          } 
          result.append("====\n"); 
         } catch (JSONException e) { 
          System.out.println("errrrror"); 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         System.out.println("Try2"+error.toString()); 

        } 
       } 
       ); 
       requestQueue.add(jsonObjectRequest); 
      } 
     }); 
    } 
} 

這裏是我的PHP代碼:

<?php 
if($_SERVER["REQUEST_METHOD"]=="POST" ) 
{ 
     include "init.php"; 
     showStudent(); 
} 

function showStudent() 
{ 
    global $con; 
    $query="SELECT * FROM student; "; 
    $result=mysqli_query($con,$query); 
    $num_of_rows=mysqli_num_rows($result); 
    $temp_arr=array(); 
    if($num_of_rows>0) 
    { 
     while($row=mysqli_fetch_assoc($result)) 
     { 
      $temp_arr[]=$row; 


     } 
    } 

    header('Content-Type:application/json'); 
    echo json_encode(array("students"=>$temp_arr)); 
    mysqli_close($con); 
} 
?> 
+0

'showurl'不返回數據 –

+0

我試了一下郵差和高級REST客戶端和返回的學生排列如圖@fahad –

回答

2

從服務器返回的響應不是有效的JSON已經開始連字

這是從服務器

JSON響應
connected{"students":[{"id":"1","firstname":"saleh","lastname":"refai","age":"333"},{"id":"2","firstname":"ali","lastname":"hariri","age":"22"}]} 

斷開連接,然後再試一次

0
@Override 
     public void onResponse(String response) { 
      try { 
       JSONObject jsonObject = new JSONObject(response); 
       JSONArray jsonArray = jsonObject.getJSONArray("students"); 
       for (int x = 0; x < jsonArray.length(); x++) { 
        JSONObject JO = jsonArray.getJSONObject(x); 
        String firstname = JO.getString("firstname"); 
       } 

      } cSONException e) { 
       e.printStackTrace(); 
      } 
+0

這是行不通的,因爲Response.Listener 需要的JSONObject作爲onResponse方法參數 –

+0

是的連接的字,我的錯。 AmirhosseinAbd93答案將解決這個問題。 – j22purikas

0

在你的JSON對象的名稱是 「stude」,但在你的代碼中使用的 「學生」

代碼:

JSONArray students=response.getJSONArray("students"); 

必須更改爲:

JSONArray students=response.getJSONArray("stude"); 
+0

沒有,這是我的錯,我試圖改變在PHP代碼名稱stude並嘗試了先進的REST客戶端,以確保它是否改變了JSON響應的頭。這不是問題的解決方案 –

相關問題