Response Listener没有调用onResponse方法

时间:2016-04-08 19:09:24

标签: android json xampp

我正在开发一个应用程序,我可以使用本地服务器XAMPP将数据插入数据库。我创建用于插入数据或显示数据的php文件。这些文件使用postman完美运行,但在Android应用程序中它工作正常。

这是代码:

package com.example.uni.jsonpractice;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity   {

    EditText name,username,password,email;
    Button insert,show;
    TextView result;
    RequestQueue requestQueue;
    String InsertUrl="http://192.168.1.201/www/insert.php";
    String ShowUrl="http://192.168.1.201:80/www/showStudent.php";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name=(EditText)findViewById(R.id.editText);
        username=(EditText)findViewById(R.id.editText2);
        password=(EditText)findViewById(R.id.editText3);
        email=(EditText)findViewById(R.id.editText4);

        result=(TextView)findViewById(R.id.textView);

        insert=(Button)findViewById(R.id.button);
        show=(Button)findViewById(R.id.button2);

        requestQueue= Volley.newRequestQueue(getApplicationContext());


        /////////////////////show button

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, ShowUrl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray students = response.getJSONArray("student");
                            for (int i = 0; i < students.length(); i++) {
                                JSONObject student = students.getJSONObject(i);
                                String name = student.getString("name");
                                String username = student.getString("username");

                                String password = student.getString("password");
                                String email = student.getString("email");

                                result.append(name + " " + username + " " + password + " " + email + "\n");
                            }

                            result.append("===\n");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
                requestQueue.add(jsonRequest);
            }
        });



        /////////////////////////insert button


        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringRequest request=new StringRequest(Request.Method.POST, InsertUrl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(getApplicationContext(),"response",Toast.LENGTH_LONG).show();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> parameters=new HashMap<String, String>();
                        parameters.put("name",name.getText().toString());
                        parameters.put("username",username.getText().toString());
                        parameters.put("password",password.getText().toString());
                        parameters.put("email",email.getText().toString());

                        return parameters;

                    }
                };
                requestQueue.add(request);
            }
        });
    }

    @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);
    }


}

check this image for postman result

0 个答案:

没有答案