Android:Json无法从mysql数据库中检索任何文件,它是空的
我是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检索图像或我必须纠正它
请帮助我纠正上述程序,感谢您宝贵的时间,我无法理解为什么它不打印任何东西,但是当我将它扩展到活动如何在片段中使用它时它正在工作
好的,为了避免添加更多评论:
-
确保
result
字符串中包含正确的JSON数据。 记录可以帮助完成类似的任务。 或者只是使用Toast
来显示字符串以validation它是否正确。 -
gv.setFilterText(s);
不会导致任何输出。 要在GUI中显示字符串,您应该使用放在布局中的TextView
项,在这种情况下GridView
,并在其上使用setText()
。