我是 android 新手,我正在使用 mysql 数据库,其中我链接 php 文件进行连接,工作正常,但我的代码没有显示任何内容,它只显示背景色黑色,而不是显示数据库中的数据
public class HomeFragment extends Fragment {
GridView gv;
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
StrictMode.enableDefaults();
gv = (GridView) rootView.findViewById(R.id.gridView_home);
getData();
return rootView;
}
public void getData(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.2/Android/App/getcu.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
gv.setFilterText("Couldnt connect to database "+ e.getMessage()); //not printing anything
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
String s = "";
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.getInt("Age")+"\n"+
"Mobile Using : "+json.getString("Mobile")+"\n\n";//+
"Artist:"+json.put("Images",true ); // not printing anything
}
gv.setFilterText(s); // not printing showing empty
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString()); }
} }
从文件夹 Android/pictures/image1 json 检索图像时出现另一个错误,未打印部署到数据库中的特定图像
"Artist:"+json.put("Images",true );
使用 json 检索图像的上述语句是否正确,或者我必须更正它
请帮助我纠正上述程序,感谢您宝贵的时间,我无法理解为什么它没有打印任何内容,但当我将它扩展到活动时它正在工作如何在片段中使用它