gpt4 book ai didi

java - Android:Json 无法从 mysql 数据库检索任何文件,它是空的

转载 作者:行者123 更新时间:2023-11-29 08:04:33 25 4
gpt4 key购买 nike

我是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 检索图像的上述语句是否正确,或者我必须更正它

请帮助我纠正上述程序,感谢您宝贵的时间,我无法理解为什么它没有打印任何内容,但当我将其扩展到 Activity 如何在 fragment 中使用它时它正在工作

最佳答案

好的,为了避免添加更多评论:

  1. 确保结果字符串中包含正确的 JSON 数据。 Logging可以帮助完成类似的任务。或者也许只是使用 Toast仅显示字符串以验证其是否正确。

  2. gv.setFilterText(s);不会导致任何输出。要在 GUI 中显示字符串,您应该使用 TextView您放入布局内部的项目,在本例中为 GridView,以及 setText()就在上面。

关于java - Android:Json 无法从 mysql 数据库检索任何文件,它是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22986878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com