爬了別人的文章用PHP顯示資料都可以顯示但在android studio上都顯示不出來
public class MainActivity extends AppCompatActivity {
String showUri = "http://127.0.0.1/loading.php";
com.android.volley.RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
final TextView txt;
txt =(TextView) findViewById(R.id.textView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.POST,showUri, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
JSONArray data = response.getJSONArray("data");
//這邊要和上面json的名稱一樣
//下邊是把全部資料都印出來
for (int i = 0; i < data.length(); i++) {
JSONObject jasondata = data.getJSONObject(i);
String id = jasondata.getString("id");
String month_free = jasondata.getString("month_free");
txt.append(id + " " + month_free + " " + " \n");
//txt是textview
}
txt.append("===\n");//把資料放到textview顯示出來
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
}
String showUri = "http://127.0.0.1/loading.php";
如果手機執行這段, 127.0.0.1 就是執行的手機, 手機並沒有架設 Web Server.
所以, 如果你是用模擬器, 應該改為:
http://10.0.2.2/loading.php
或是Web Server的非本機回路 ip, 例如: 192.168.x.x, 或是真實 ip
如果是用實機執行, 則必須是該Web Server的真實 ip