2015-06-15 34 views
0

我是非常非常新的android應用程序開發,我需要從我本地的xampp mysql數據庫中獲取數據。這些是我不斷收到的錯誤消息。它爲什麼說它是一個字符串?

06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:96) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at org.json.JSONArray.<init>(JSONArray.java:108) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at com.example.shirlyn.attendancesystem.StudentJSONParser.parseFeed(StudentJSONParser.java:18) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at com.example.shirlyn.attendancesystem.MainActivity$ThreadClass.onPostExecute(MainActivity.java:111) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at com.example.shirlyn.attendancesystem.MainActivity$ThreadClass.onPostExecute(MainActivity.java:95) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:636) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at android.os.AsyncTask.access$500(AsyncTask.java:177) 
06-15 15:12:31.472 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at android.os.Looper.loop(Looper.java:135) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
06-15 15:12:31.473 28252-28252/com.example.shirlyn.attendancesystem W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

這是我的PHP文件get_all_products.php

<?php // array for JSON response 
$response = array(); 

// include db connect class 
require_once __DIR__ . '/db_connect.php'; 

// connecting to db 
$db = new DB_CONNECT(); 

// get all products from products table 
$result = mysql_query("SELECT * FROM student") or die(mysql_error()); 

// check for empty result 
if (mysql_num_rows($result) > 0) { 
// looping through all results 
// products node 
$response = array(); 

while ($row = mysql_fetch_assoc($result)) { 
    // temp user array 
    $response[] = $row; 
} 
// success 

// echoing JSON response 
echo json_encode($response); 
} else { 
// no products found 
$response["success"] = 0; 
$response["message"] = "No students found"; 

// echo no users JSON 
header(‘Content-type: application/json’); 
echo json_encode($response); 
} 
?> 

get_all_products.php的結果,如果我在我的瀏覽器上運行它,它似乎正確的...

[{"student_id":"TP028616","student_name":"Shirlyn Hoe Yee Lyn","student_username":"lyn","student_password":"123"},{"student_id":"TP033500","student_name":"Ryan Teh Hoon Meng","student_username":"teh","student_password":"123"}] 

這是我在我的android應用程序中的java類StudentJSONParser.java

package com.example.shirlyn.attendancesystem; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.List; 

public class StudentJSONParser { 
    public static List<Student> parseFeed(String content){ 

     try { 
      System.out.println("begin parsing"); 
      JSONArray ar = new JSONArray(content); 
      List<Student> studentList = new ArrayList<>(); 

      for(int i = 0 ; i < ar.length() ; i++){ 
       System.out.println("loop " + i); 
       JSONObject obj = ar.getJSONObject(i); 
       Student student = new Student(obj.getString("student_id"), obj.getString("student_name"), obj.getString("student_username"), obj.getString("student_password")); 

       studentList.add(student); 
       System.out.println("complete loop " + i); 
      } 
      System.out.println("complete parsing"); 

      return studentList; 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      System.out.println("error parsing"); 

      return null; 
     } 

    } 
} 

所以這是與.php文件不返回jsonarrays的問題?或我的應用程序的邏輯是錯誤的?感謝所有提前回復。

這是我的AsyncTask類

public class MainActivity extends ActionBarActivity implements View.OnClickListener { 
private Button loginbtn; 
private EditText usernameTxt, passwordTxt; 
private ProgressBar pb; 

private List<Student> studentslist; 

public final static String readStudentphp = "http://192.168.0.103/webservice/get_all_products.php"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    OnClickButtonListener(); 
    initLogin(); 
    pb = (ProgressBar) findViewById(R.id.progressBar); 
    pb.setVisibility(View.INVISIBLE); 
} 

private void initLogin(){ 
    usernameTxt = (EditText) findViewById(R.id.usernameTxt); 
    passwordTxt = (EditText) findViewById(R.id.passwordTxt); 
} 

private void OnClickButtonListener(){ 
    loginbtn = (Button) findViewById(R.id.login); 
    loginbtn.setOnClickListener(this); 
} 

private void requestData(String uri){ 
    ThreadClass t = new ThreadClass(); 
    t.execute(uri); 
} 

@Override 
public void onClick(View v) { 
    requestData(readStudentphp); 
    //Intent intent = new Intent("com.example.shirlyn.attendancesystem.MainMenu"); 
    //startActivity(intent); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

private class ThreadClass extends AsyncTask<String, String, String>{ 
    @Override 
    protected void onPreExecute(){ 
     pb.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String content = HTTPManager.getData(params[0]); 

     return content; 
    } 

    @Override 
    protected void onPostExecute(String result){ 
     System.out.println("Before parsing"); 
     studentslist = StudentJSONParser.parseFeed(result); 
     System.out.println("After parsing"); 
     if(studentslist != null && studentslist.size() > 0) 
      System.out.println(studentslist.get(0).getStudent_name()); 
     pb.setVisibility(View.INVISIBLE); 
    } 
} 

}

+1

剛剛離開你的標題我猜你可能需要在發送JSON的時候發送適當的頭文件'application/json'什麼是確切的錯誤? – ficuscr

+0

你缺少'header('Content-type:application/json');'當有結果時,我會建議使用一個簡單的響應類來處理這個問題。 – Perry

+0

這是一個錯誤,我在我的PHP文件中刪除它。感謝您指出它 –

回答

1

貌似你試圖解析HTML的JSON(基於你的錯誤<br標籤)。

<br of type java.lang.String cannot be converted to JSONArray

仔細檢查你正在試圖解析,以確保您得到JSON從終點回到輸出。可能是接受標題或類似的東西。

+0

所以我的PHP文件沒有錯?從json數組中,我想將它們保存在一個數組中。 –

+0

@ user3244616如果上述答案解決了您的問題,您可以接受答案。你的意思是什麼,沒有錯,我的PHP文件? – Perry

+0

目前尚不清楚您打算打算執行的PHP代碼。如果您將整個字符串視爲您想要解析爲JSON,則應該能夠找出返回該HTML的內容。我看到的PHP代碼看起來並不會返回HTML,所以它並不代表您正在瀏覽它,或者得到了@MarcB建議的錯誤。 – pherris

相關問題