Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php android应用程序中未从服务器获取JSON数据_Php_Android_Json_Android Volley - Fatal编程技术网

Php android应用程序中未从服务器获取JSON数据

Php android应用程序中未从服务器获取JSON数据,php,android,json,android-volley,Php,Android,Json,Android Volley,所以我刚刚开始玩JSON和截击。我已经使用XAMMP创建了一个服务器,并添加了生成JSON数据所需的php脚本。它在浏览器中显示良好。但是,在模拟器上运行应用程序时。数据永远不会被检索,进度对话框也会不断加载,因此应用程序永远不会崩溃。我采用的代码如下。我错过了什么 public class Config { public static final String DATA_URL2= "http://10.0.2.2:8080/webservice/index.php"; public st

所以我刚刚开始玩JSON和截击。我已经使用XAMMP创建了一个服务器,并添加了生成JSON数据所需的php脚本。它在浏览器中显示良好。但是,在模拟器上运行应用程序时。数据永远不会被检索,进度对话框也会不断加载,因此应用程序永远不会崩溃。我采用的代码如下。我错过了什么

public class Config {


public static final String DATA_URL2= "http://10.0.2.2:8080/webservice/index.php";
public static final String KEY_ID = "ID";
public static final String KEY_NAME = "Name";
public static final String KEY_SURNAME = "Surname";
public static final String JSON_ARRAY = "result";}

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;

private ProgressDialog loading;

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

    editTextId = (EditText) findViewById(R.id.editTextId);
    buttonGet = (Button) findViewById(R.id.buttonGet);
    textViewResult = (TextView) findViewById(R.id.textViewResult);

    buttonGet.setOnClickListener(this);
}

private void getData() {

    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

    String url = Config.DATA_URL2.toString();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
    String id="";
    String name="";
    String surname = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        id = collegeData.getString(Config.KEY_ID);
        name = collegeData.getString(Config.KEY_NAME);
        surname = collegeData.getString(Config.KEY_SURNAME);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("ID:\t"+id+"\nName:\t" +name+ "\nSurname:\t"+ surname);
}

@Override
public void onClick(View v) {
    getData();
}}
另外。。。这是我的php脚本

<?php

define('HOST','localhost');
  define('USER','ruchen');
  define('PASS','lollatjie');
  define('DB','webservice1');

 $con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from users where surname = 'Miller'";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'surname'=>$row[2]
));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);
?>

401表示您无权访问此url

从这个链接

10.4.2未经授权


请求需要用户身份验证。响应必须包括WWW Authenticate标头字段(第14.47节),其中包含适用于请求资源的质询。客户可以使用合适的授权标头字段重复请求(第14.8节)。如果请求中已包含授权凭据,则401响应表示已拒绝这些凭据的授权

我不确定您为什么会得到错误代码401,但我注意到您的代码中有一些奇怪的地方。为什么要发出字符串请求而不是JsonObject请求

毕竟,您是从php服务中获取JSON数据的

将其更改为JsonObjectRequest,因为Json的开头是一个名为“result”的对象,该对象包含一个数组

我在我的应用程序中也做了同样的事情,我的截击下载看起来是这样的:

 theEvents = new ArrayList<Event>();
    requestQueue = Volley.newRequestQueue(getActivity());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, urlService, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {


                        id = response.getString(Config.KEY_ID); // response is now the actual object called "result" from your JSON data.
                          // do something with the ID
                        }
.........etc
theEvents=newarraylist();
requestQueue=Volley.newRequestQueue(getActivity());
JsonObjectRequest JsonObjectRequest=新JsonObjectRequest
(Request.Method.GET,urlService,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
id=response.getString(Config.KEY_id);//response现在是JSON数据中名为“result”的实际对象。
//用身份证做点什么
}
等
{"result":[{"id":"7","name":"Bob","surname":"Miller"}]}
 theEvents = new ArrayList<Event>();
    requestQueue = Volley.newRequestQueue(getActivity());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, urlService, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    try {


                        id = response.getString(Config.KEY_ID); // response is now the actual object called "result" from your JSON data.
                          // do something with the ID
                        }
.........etc