从Mysql到android的数据检索不会出现

时间:2017-02-21 22:45:14

标签: android json

我想将mysql中的json数据检索到我的应用程序中我认为所有步骤都可以,但没有结果 我从另一个活动

传递了<?xml version="1.0" encoding="UTF-8"?><manifest package="com.package.my.domain"> <application xmlns:android="http://schemas.android.com/apk/res/android" android:name="AlarmMobile"> <activity android:name=".activity.DeepLinkActivity"> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="http" android:host="hostTwo.com" android:path="/page.aspx"/> </intent-filter> </activity> </application> </manifest>
passingitem

配置文件

    public class SingleNewsDetails extends Activity {


    TextView newsdetails;
    ImageView newspic;
    TextView newsaddreess;
    int passingitem;
RequestQueue requestQueue;
String url;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singlenewsdetails);
        newsdetails = (TextView) findViewById(R.id.news_details_text);
        newspic = (ImageView) findViewById(R.id.newspic);
        newsaddreess = (TextView) findViewById(R.id.news_address_text);
        Intent intent = getIntent();
        passingitem = intent.getIntExtra("news_id",0);
        getData();
    }

    public void getData() {
        url = Config.DATA_URL + String.valueOf(passingitem);
        Toast.makeText(this,url,Toast.LENGTH_SHORT).show();
        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 address = respons.getString("address");
  //                              String image = respons.getString("image");
                                String desc = respons.getString("desc");
                                newsaddreess.setText(address);
                                newsdetails.setText(desc);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", "ERROR");
            }
        }
        );
        requestQueue.add(jsonObjectRequest);
    }

}

传递项目是从此活动传递的

    public class Config {
        public static final String DATA_URL = "http://giclub.esy.es/News.php?id=";
        public static final String KEY_ID = "id";
        public static final String KEY_ADDRESS = "address";
        public static final String KEY_IMAGE = "image";
        public static final String KEY_DESC = "desc";
        public static final String JSON_ARRAY = "result";
    }

singlenewsdetails.xml

   public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
    {
        Intent intent = new Intent(this,SingleNewsDetails.class);
        String selecteditem = String.valueOf(listView.getItemIdAtPosition(position));
        Toast.makeText(NewsFragmentActivity.this,selecteditem,Toast.LENGTH_LONG).show();
        intent.putExtra("news_id",position);
        startActivity(intent);
    }

并显示该错误

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:id="@+id/news_address_text"
    android:layout_height="25dp"
    android:textSize="20dp"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/single_news_image"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/news_details_text"
        android:layout_gravity="right"
        android:textSize="16dp"/>

</LinearLayout>

我试图烘烤json url并在屏幕上显示但没有显示数据有什么不对?抱歉英文不好

1 个答案:

答案 0 :(得分:0)

你的url变量是在活动实例化时分配的,没有设置passItem,这意味着你可能总是使用这个URL:

  

http://giclub.esy.es/News.php?id=0

在响应中返回此null:

  

{ “allstudents”:[{ “desc” 的日期null, “图像”:空}]}

我建议您将getData()方法修改为getData(String url)

然后将其称为: getData(Config.DATA_URL + String.valueOf(passingitem));