duannao8450 2015-06-15 19:34
浏览 94
已采纳

为什么它说它是一个字符串?

I am very very new in android app development and I need to get data from my local xampp mysql database. These are the error message I keep getting.

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)  

This is my php file 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);
}
?>  

The results of get_all_products.php if I run it on my browser and it seems right...

[{"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"}]

This is my java class in my android app 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;
        }

    }
}

so is this the problem with the .php file not returning jsonarrays? or the logic of my app is wrong? Thanks for all the replies in advance.

this is my asynctask class

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条回答 默认 最新

  • douyong5825 2015-06-15 19:39
    关注

    Looks like you are trying to parse HTML as JSON (based on the <br tag in your error).

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

    Double check the output you are trying to parse to make sure you are getting JSON back from your end point. Might be a accepts header or something like that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型