سلام
(برنامه بدون خطا اجرا میشه و پیام Online بودن هم ظاهر میشه یعنی اینترنت اوکیه ولی فایل XML نشون داده نمیشه)
یه برنامه تو اندروید استودیو نوشتم برای خوندن اطلاعات فایل XML به ادرس ذیل:
http://services.hanselandpetal.com/feeds/flowers.xml
package ir.ac.soore.kavehnorozi.soore;import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLCoection;
import java.net.URL;/**
* Created by kavehnorozi on 01/08/2016.
*/
public class HttpManager {
public static String getData(String uri) throws IOException {
BufferedReader reader= null;
try{
URL url = new URL(uri);
HttpURLCoection con = (HttpURLCoection) url.openCoection();
StringBuilder sb=new StringBuilder();
reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line=reader.readLine())!=null){
sb.append(line+"n");
}retu sb.toString();
}catch (Exception e){
e.printStackTrace();
retu null;
}finally {
if(reader!=null) reader.close();
}
}
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_coordinator_ntb);
TextView txt = (TextView) findViewById(R.id.txtxml);
initUI();
if(isonline()) {
TextView text= (TextView) findViewById(R.id.txt_vp_item_list);
try {Toast.makeText(this," Online",Toast.LENGTH_SHORT).show();
String content= HttpManager.getData("http://services.hanselandpetal.com/feeds/flowers.xml");txt.setText(content);
} catch (IOException e) {
e.printStackTrace();
}}else Toast.makeText(this,"not Online",Toast.LENGTH_SHORT).show();
}
protected boolean isonline(){
CoectivityManager cm = (CoectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if(netinfo!=null && netinfo.isCoectedOrCoecting())
{
retu true;
}
else retu false;
}
برچسب:
نویسنده: محمد رضا جوادیان