无法在android studio中的模拟器上获取json数据

时间:2015-09-28 10:03:05

标签: android json

我正在使用android studio,mysql数据库和notepad ++来处理php文件。 该数据库有一个名为登录的表,并有3个属性:

  1. ID
  2. 姓名
  3. 名字
  4. 所有类型都是varchar 当我尝试执行android和php的代码时,我没有收到任何错误,代码执行成功,但数据库中的数据没有显示在模拟器上。

       *******************Android code************************
    package com.example.digi.college;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    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 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 firstname, lastname, id;
    Button insert, show;
    TextView result;
    RequestQueue requestQueue;
    String inserturl = "http://192.168.1.104/android/register.php";
    String displayurl = "http://192.168.1.104/android/display.php";
    String hello="hello how are you";
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        id = (EditText) findViewById(R.id.editText);
        firstname = (EditText) findViewById(R.id.editText2);
        lastname = (EditText) findViewById(R.id.editText3);
        insert = (Button) findViewById(R.id.button);
        show = (Button) findViewById(R.id.button2);
        result = (TextView) findViewById(R.id.textView);
    
        requestQueue = Volley.newRequestQueue(getApplicationContext());
    
    
    
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
    
    
               JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                        displayurl, new Response.Listener<JSONObject>() {
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray students = response.getJSONArray("students");
    
                                for (int i = 0; i < students.length(); i++) {
                                    JSONObject login = students.getJSONObject(i);
    
                                    String id = login.getString("id");
                                    String firstname = login.getString("firstname");
                                    String lastname = login.getString("lastname");
    
                                    result.append(id+" "+firstname+" "+lastname+"\n");
    
                                }
                                result.append("===\n");
    
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
    
                        }
                }, new Response.ErrorListener() {
    
                    public void onErrorResponse(VolleyError error) {
    
                    }
    
                });
                requestQueue.add(jsonObjectRequest);
            }
        });
    
        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 s) {
    
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
    
                    }
    
            }){
    
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                       Map<String,String> parameters = new HashMap<String, String>();
                        parameters.put("id",id.getText().toString());
                        parameters.put("firstname",firstname.getText().toString());
                        parameters.put("lastname",lastname.getText().toString());
    
                        return parameters;
                    }
                };
                requestQueue.add(request);
            }
        });
    
        }
    
      }
    
    
    
     *****************Php Code for insert.php******************
    
    
    
    <?php
     if($_SERVER["REQUEST_METHOD"] == "POST")
     {
      $id=$_POST['id'];
       $firstname = $_POST['firstname'];
      $lastname = $_POST['lastname'];
    
      $db = mysql_connect("localhost", "root", "");
      mysql_select_db("ntu", $db);
    
      $sql = mysql_query("INSERT INTO login (id,firstname,lastname) VALUES           ('$id','$firstname', '$lastname')");
    
     mysql_close($db);
    
      }
    
     ?>
    
    
      *************Php code for display.php*************
    
    
    <?php
    
    
    
       $db = mysql_connect("localhost", "root", "");
       mysql_select_db("ntu", $db);
    
    
       $sql = mysql_query("SELECT * FROM login");
    
    
      $a = array(); 
    
    
       $index = 0;
      while($row = mysql_fetch_assoc($sql)) 
       {
     $a[$index] = $row;
     $index++;
       }
    
    
    
    
       echo json_encode(array("students"=>$a));
      mysql_close($db);
    
    
    
      ?>
    

1 个答案:

答案 0 :(得分:0)

我认为你错过了androidmanifeast.xml文件中的互联网权限,并获得了互联网许可并进行了检查。