如何使用JSON在一行上显示每个查询结果?

时间:2016-06-18 22:53:05

标签: android mysql json

我正在开发一个接收客户订单的应用程序。      我必须显示三列:名称,位置,顺序     当我运行应用程序时,在订单字段下,信息显示为:

Tomato quantity 4,Strawberry quantity 6

 **How to display them like :**

 *Tomato quantity 4

 Strawberry quantity 6*

I mean each order name with the quantity on a single line

这是我收到信息的方式:

enter image description here 这是我的Json代码:

public class BackgroundTask extends AsyncTask<Void,Order,Void> {
    Context ctx;
    Activity activity;
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ProgressDialog progressDialog;
    ArrayList<Order>arrayList= new ArrayList<>();
    public BackgroundTask(Context ctx)
    {
    this.ctx=ctx;
    activity=(Activity)ctx;
    }
    String json_string = "\\";

@Override
protected void onPreExecute() {
    recyclerView=(RecyclerView)activity.findViewById(R.id.recyclerView);
    layoutManager=new LinearLayoutManager(ctx);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter=new RecyclerAdapter(arrayList);
    recyclerView.setAdapter(adapter);
    progressDialog = new ProgressDialog(ctx);
    progressDialog.setTitle("Please Wait..");
    progressDialog.setMessage("List is loading..");
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    progressDialog.show();
}


@Override
protected void onProgressUpdate(Order... values) {
    arrayList.add(values[0]);
    adapter.notifyDataSetChanged();
}


@Override
protected void onPostExecute(Void result) {
    progressDialog.dismiss();

}

@Override
protected Void doInBackground(Void... params) {
    try
    {
        URL url = new URL(json_string);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();


        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line + "\n\t");
        }

        httpURLConnection.disconnect();


        String json_string = stringBuilder.toString().trim();
        JSONObject jsonObject = new JSONObject(json_string);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");
        int count=0;
        while(count<jsonArray.length())
        {
            JSONObject JO=jsonArray.getJSONObject(count);
            count++;
            Order order = new Order(JO.getString("name"),JO.getString("location"),JO.getString("order_name"));
            publishProgress(order);
            Thread.sleep(1000);

        }

        Log.d("JSON STRING", json_string);


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return  null;

}
}

我的PHP代码

<?PHP
    $con= mysqli_connect($host,$user,$password,$db);
    $query="select * from Restaurant;";
    $result=mysqli_query($con,$query);
    $response=array();

    while($row=mysqli_fetch_array($result))
    {   
    array_push($response,array('name'=>$row[0],'location'=>$row[1],'order_name'= >$row[2]));
    }
    mysqli_close($con);
    echo json_encode(array('server_response'=>$response));
?>

0 个答案:

没有答案