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 + "
");
}
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")+"
"+
"Age : "+json.getInt("Age")+"
"+
"Mobile Using : "+json.getString("Mobile")+"
";//+
"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()); }
} }
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 );
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:
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);
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.