Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中分离JSON数组中的对象_Java_Php_Android_Arrays_Json - Fatal编程技术网

在java中分离JSON数组中的对象

在java中分离JSON数组中的对象,java,php,android,arrays,json,Java,Php,Android,Arrays,Json,我有下面的PHP代码,它将每三个条目作为不同的对象添加到数组中 //Read the username and password from ServerRequest class $username = $_POST["username"]; //Select all rows when the username match the input $statement = mysqli_prepare($con, "SELECT * FROM Chore WHERE usern

我有下面的PHP代码,它将每三个条目作为不同的对象添加到数组中

  //Read the username and password from ServerRequest class
$username = $_POST["username"];   

    //Select all rows when the username match the input
$statement = mysqli_prepare($con, "SELECT * FROM Chore WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);

    //store the results of the previous query and create a variable for each column returned
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$chore_name ,$point_value, $parent_username, $child_username, $created, $points_given);
$chore = array();

    //store the previous variables you created in the array (only the ones you want to use)

    while(mysqli_stmt_fetch($statement)) {
      $t = new stdClass();
      $t->chore_name     = $chore_name;
      $t->child_username = $child_username;
      $t->point_value    = $point_value;

      $chore[] = $t;
    }

    //encode the array in JSON format
echo json_encode($chore);

//close the statement 
mysqli_stmt_close($statement);

    //Close the database connection 
mysqli_close($con);
但是,我不确定如何在活动中创建这些列表视图。我在xml文件中有这个

 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/choreList"
    android:textSize="20dp"
    android:typeface="sans"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/currentChores"
    android:paddingBottom="10dp"
    android:paddingTop="50dp">
</ListView>

下面是我到目前为止的代码,但我很确定它是完全错误的,并且不知道应该把它放在代码的哪个部分

     Bundle extras = getIntent().getExtras();
    final String user = extras.getString("username");


    //The URL is the location of the PHP file to validate a user
    String requestURL = SERVER_ADDRESS+"FetchChoreData.php";

    URL url;
    Chore returnedChore = null;
    try {
        //Opens the connection to the PHP files
        //Sets the conditions of the connection
        url = new URL(requestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(CONNECTION_TIMEOUT);
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        //Opens an output stream to send the data to be verified
        // and then closes the all output streams and flushes the output writer
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("username",user);

        String query = builder.build().getEncodedQuery();

        writer.write(query);
        writer.flush();
        writer.close();
        os.close();
        //saves the response code to ensure connection was succesful
        int code = conn.getResponseCode();
        Log.d("code", Integer.toString(code));

        //Opens an input stream to retrieve verified data back from the server
        //Starts a String Builder to read the data off the input
        //Closes the BufferedReader once it has finished building the string
        InputStream responseStream = new BufferedInputStream(conn.getInputStream());
        BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = responseStreamReader.readLine()) != null)
            stringBuilder.append(line + "/n");
            responseStreamReader.close();

            String response = stringBuilder.toString();
            Log.d("response",response);

            JSONObject jsonResponse = new JSONObject(response);

            //Creates a JSON object from the string
            ArrayList<String> chores = new ArrayList<String>();
            for(int i=0; i < chores.length() ; i++) {
                JSONObject json_data = jsonResponse.getJSONObject(i);

                String chore=json_data.getString("chore_name");
                String username=json_data.getString("child_username");
                int points=json_data.getInt("point_value");
                String currentChore = chore + "\t" + username + "\t" + points;
                chores.add(currentChore);
                Log.d("Output", currentChore);


            ArrayAdapter<String> choreAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, chores));
            setListAdapter(choreAdapter);

            ListView listView = (ListView) findViewById(R.id.choreList);
            listView.setAdapter(choreAdapter);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } catch(JSONException e){
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    }
Bundle extras=getIntent().getExtras();
最终字符串user=extras.getString(“用户名”);
//URL是用于验证用户的PHP文件的位置
String requestURL=SERVER_ADDRESS+“FetchChoreData.php”;
网址;
Chore returnedChore=null;
试一试{
//打开与PHP文件的连接
//设置连接的条件
url=新url(请求url);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(连接超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
//打开输出流以发送要验证的数据
//然后关闭所有输出流并刷新输出写入程序
OutputStream os=conn.getOutputStream();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(os,“UTF-8”));
Uri.Builder=新的Uri.Builder()
.appendQueryParameter(“用户名”,用户);
字符串查询=builder.build().getEncodedQuery();
writer.write(查询);
writer.flush();
writer.close();
os.close();
//保存响应代码以确保连接成功
int code=conn.getResponseCode();
Log.d(“code”,Integer.toString(code));
//打开输入流以从服务器检索已验证的数据
//启动字符串生成器以从输入中读取数据
//完成构建字符串后关闭BufferedReader
InputStream responseStream=新的BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader=新的BufferedReader(新的InputStreamReader(responseStream));
弦线;
StringBuilder StringBuilder=新的StringBuilder();
而((line=responseStreamReader.readLine())!=null)
stringBuilder.append(第+“/n行”);
responseStreamReader.close();
字符串响应=stringBuilder.toString();
Log.d(“响应”,响应);
JSONObject jsonResponse=新的JSONObject(响应);
//从字符串创建JSON对象
ArrayList琐事=新建ArrayList();
for(int i=0;i
可能重复我认为问题出在这里:JSONObject jsonResponse=新的JSONObject(response);您需要使用jsonResponse.getJSONArray(“数组名称”);JSONObject之后jsonResponse=新的JSONObject(response);