安卓:JSON是无法检索从mysql数据库中的任何文件,它是空的它是、数据库中、文件、JSON

由网友(北巷不夏)分享简介:我是新来的Andr​​oid和我使用​​的MySQL数据库,在那里我链接连接这是工作的罚款php文件,但我的code未显示任何东西它只是显示背景色而不是黑色显示的数据数据库i am new to android and i am using mysql database where i am linking php...

我是新来的Andr​​oid和我使用​​的MySQL数据库,在那里我链接连接这是工作的罚款php文件,但我的code未显示任何东西它只是显示背景色而不是黑色显示的数据数据库

i am new to android and i am using mysql database where i am linking php file for connection which is working fine but my code is not displaying anything it is only showing background color black instead of displaying the data from the database

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")+"nn";//+
               "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()); }

}  }

多了一个错误,而从文件夹中的Andr​​oid /图片/ JSON此搜索检索图像不打印部署到数据库中的特定图像

one more error while retrieving images from folder Android/pictures/image1 json is not printing the particular image deployed into database

"Artist:"+json.put("Images",true ); 

是上面的语句正确检索使用JSON的图像或我必须纠正

is the above statement correct to retrieve the images using json or i have to correct it

请帮我纠正上述方案感谢您的宝贵时间我不能够理解为什么它没有打印任何东西,但它工作时,我把它扩展到活动如何使用它的片断

please help me to correct the above program thanks for your valuable time i am not able to understand why it is not printing anything but it is working when i extend it to activity how to use it in fragment

推荐答案

确定,以避免增加更多评论:

Ok, to avoid adding more comments:

请确保你有你的结果字符串正确的JSON数据。 记录可以帮助这样的任务。或者只是使用 吐司 只显示字符串以验证它是正确的。

Make sure you have the correct JSON data in your result string. Logging can help with tasks like that. Or maybe just use a Toast to just display the string to verify it's correct.

gv.setFilterText(S); 将不可以造成任何输出。要在您应该使用 的TextView 你把在的布局,的GridView 在这种情况下,和 的setText() 就可以了。

gv.setFilterText(s); will not cause any output. To display strings in the GUI you should use a TextView item that you put inside your layout, GridView in this case, and setText() on it.

阅读全文

相关推荐

最新文章