org.json.JSONException,无法获取查询结果

时间:2019-11-01 17:52:20

标签: android json

我尝试构建一个Android应用程序,其中:
1。插入代码
2。该代码发送到php并处理mysql查询
3。将有关json结果的查询返回给android

我尝试了所有有关此问题的答案,但没有一个能解决问题

s4

和php文件:

public class MainActivity extends AppCompatActivity {

EditText  codice;
Button show;
RequestQueue requestQueue;
String showUrl = "http://192.168.1.2/MyApi/Api.php"; 

TextView result, mostra;
ProgressBar progressBar;
Handler hdlr = new Handler();

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




    show = (Button) findViewById(R.id.showstudents);
    result = (TextView) findViewById(R.id.textView);
    mostra = (TextView) findViewById(R.id.viewcode);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);


    requestQueue = Volley.newRequestQueue(getApplicationContext());

    show.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            StringRequest request = new StringRequest(Request.Method.POST, showUrl, 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) {
                    Toast.makeText(getApplicationContext(), error.toString(),Toast.LENGTH_LONG).show();

                }
            }) {

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String,String> parameters  = new HashMap<String, String>();

                    parameters.put("codice",codice.getText().toString());

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



            progressBar.setVisibility(View.VISIBLE);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    progressBar.setVisibility(View.GONE);
                }
            },3000);


            ////////////////
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, showUrl,null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(getApplicationContext(), response.toString(),Toast.LENGTH_LONG).show();

                    try {
                        JSONArray students = response.getJSONArray("students");

                        for (int i = 0; i < students.length(); i++) {
                            JSONObject student = students.getJSONObject(i);

                            String codice = student.getString("codice");
                            String website = student.getString("website");
                            String email = student.getString("email");
                            result.append(codice + "\n" + website +"\n"+ email);


                        }




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

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(),Toast.LENGTH_LONG).show();

                }
            });
            requestQueue.add(jsonObjectRequest);
        }
    });
 }

 }

如果$ codice的值不是从android发送的,而只是写在php代码中,则一切正常。
$ codice = 123;
当我使用
$ codice = $ _POST ['codice'];
得到错误org.json.JSONException:类型为

的值
  

java.lang.string无法转换为jsonobject

0 个答案:

没有答案