0
I created an android app that consumes data from a webservice Rest made in Php, so far everything working perfectly. What I want now is to show a message on the screen if you do not have Internet access or inform the user to turn on the Internet in case he forgets. Someone can help me with that. Thanks in advance :).
Follow my code here.
public class MainActivity extends AppCompatActivity {
String myJSON;
Handler handler;
private Context context;
private static final String TAG_RESULTS="resultado";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_ADD ="address";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list= (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
public void restartActivity(View view)
{
// do your work Here
Intent intent= new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
BufferedReader inputStream = null;
String result = null;
try {
URL url = new URL("http://10.127.127.1/ws/aula.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
inputStream = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = inputStream.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
}
}
}
I put this code in the application and it worked by half, it notifies when it is connected to the Internet, so far so good, but I’m not getting to show the message " Could not connect" because the application stops working when it can not connect to the Internet. There’s something wrong with my initial code and I can’t find it. Help??
– Yanik Santos
This code has to run on Oncreate before calling your getData().
– Márcio Oliveira
How can I do that??
– Yanik Santos
I use it in
OnPostExecute
and works normal, both with and without internet, @Yaniksantos. But try to do as Márcio said, put inonCreate
. If possible, put the error that gives also.– Marceloawq
I put in Onpostexecute and it worked well.. Worth the trouble was in my initial code.
– Yanik Santos