使用Volley将数据从PHP显示到列表视图中

时间:2017-03-14 15:01:25

标签: php android android-studio android-volley

我正在尝试使用PHP将数据存储在MySQL中的服务器上,并在Android的List View中显示。我在Android上使用以下代码:

public class MainActivity extends AppCompatActivity {

RequestQueue requestQueue;
String url = "http://blood.usa.cc/Show.php";
TextView textView;

ListView listView;

ArrayList<listitme> listitmes = new ArrayList<listitme>();


////Send_Data
EditText EName, EInfo;

///Send_Img
ImageView imgV;
String encodimg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    listView = (ListView) findViewById(R.id.listView);

    imgV = (ImageView) findViewById(R.id.imgV);

    EName = (EditText) findViewById(R.id.ETXT_Name);
    EInfo = (EditText) findViewById(R.id.ETXT_Info);

    requestQueue = Volley.newRequestQueue(this);
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONArray jsonArray = response.getJSONArray("allstudents");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject respons = jsonArray.getJSONObject(i);
                            String id = respons.getString("id");
                            String name = respons.getString("name");
                            String info = respons.getString("info");
                            String img = respons.getString("img");
                           textView.append("\n" + id + " - " + name + "\n" + info + "\n" + "-------------" + "\n");
                            listitmes.add(new listitme(id, name, info, img));
                            listAllItme();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {


            Log.e("VOLLEY", "ERROR");
        }
    }

    );
    requestQueue.add(jsonObjectRequest);
}

public void listAllItme() {
    listAdpter lA = new listAdpter(listitmes);
    listView.setAdapter(lA);
}


class listAdpter extends BaseAdapter {
    ArrayList<listitme> listA = new ArrayList<listitme>();

    public listAdpter(ArrayList<listitme> listA) {
        this.listA = listA;
    }

    @Override
    public int getCount() {
        return listA.size();
    }

    @Override
    public Object getItem(int position) {
        return listA.get(position).id;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.row_itm, null);
        TextView id = (TextView) view.findViewById(R.id.textView_id);
        TextView name = (TextView) view.findViewById(R.id.textView_name);
        TextView info = (TextView) view.findViewById(R.id.textView_info);
        ImageView img = (ImageView) view.findViewById(R.id.imgV);
        id.setText(listA.get(position).id);
        name.setText(listA.get(position).name);
        info.setText(listA.get(position).info);

        Picasso.with(MainActivity.this).load("http://blood.usa.cc/0212/m/" + listA.get(position).img).into(img);

        return view;
    }
}

///إرسال البيانات إلى السيرفر
//Send_Data_to_Serveries
public void btn_Send_Data(View view) {

    Bitmap Bimg = ((BitmapDrawable) imgV.getDrawable()).getBitmap();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Bimg.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    encodimg = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);


    String Ename = EName.getText().toString();
    String Einfo = EInfo.getText().toString();

    Response.Listener<String> responseLisener = new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                boolean success = jsonResponse.getBoolean("success");
                if (success) {
                    Toast.makeText(MainActivity.this, "تم إرسال البيانات بنجاح", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "يوجد خطأ لم يتم إرسال البيانات...", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    Send_Data send_Data = new Send_Data(Ename, Einfo,encodimg, responseLisener);
    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    queue.add(send_Data);

}

///Open Image **
public void btn_Pick_Photo(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 100);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 100 && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        imgV.setImageURI(uri);
    }
}

这是我的PHP代码:

<?php
include("config.php");

header('Content-Type: text/html; charset=utf-8');

$result = mysql_query("SELECT * FROM students ") ;

$stu = array();
while( $row = mysql_fetch_array( $result ))
{
$stu[] = $row;
}

print "{allstudents : ".json_encode($stu,JSON_UNESCAPED_UNICODE)."}";

?>

当我的代码运行时,我在日志中看到错误

  

E / VOLLEY:ERROR

正如您在屏幕截图中看到的那样

[data must appear under buttons]

enter image description here enter image description here

我做错了什么?如何解决此错误?

0 个答案:

没有答案