這裏是代碼,但它不能得到json數據,雖然瀏覽器顯示json數據時,我打開它爲什麼?php代碼工作正常,但當在Android正試圖把它拿來都說我取空我的android應用程序不geting從服務器json數據
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
resultview=(TextView)findViewById(R.id.resulttext);
getdata();
}
public void getdata()
{
String result=null;
InputStream inputstream=null;
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://127.0.0.1/getallcustomers.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity httpentity=response.getEntity();
inputstream= httpentity.getContent();
}
catch(Exception ex)
{
resultview.setText(ex.getMessage()+"at 1st exception");
}
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!= null)
{
sb.append(line +"\n");
}
inputstream.close();
result=sb.toString();
}
catch(Exception ex)
{
resultview.setText(ex.getMessage()+"at 2nd exception");
}
try{
String s="test :";
JSONArray jarray=new JSONArray(result);
for(int i=0; i<jarray.length();i++)
{
JSONObject json=jarray.getJSONObject(i);
s= s +
"Name : "+json.getString("firstname")+" "+json.getString("lastname")+"\n"+
"age :"+json.getString("age")+"\n"+
"phone : "+json.getString("phone");
}
resultview.setText(s);
}
catch(Exception ex)
{
this.resultview.setText(ex.getMessage()+"at 3rd exception");
}
}
}
當你直接訪問你的php文件時,你看到了什麼結果? –