kaveh.norozi
دوشنبه 11 مرداد 1395, 14:29 عصر
سلام
(برنامه بدون خطا اجرا میشه و پیام Online بودن هم ظاهر میشه یعنی اینترنت اوکیه ولی فایل XML نشون داده نمیشه)
یه برنامه تو اندروید استودیو نوشتم برای خوندن اطلاعات فایل XML به ادرس ذیل:
http://services.hanselandpetal.com/feeds/flowers.xml
یه کلاس نوشتم به اسن HttpManager به شکل ذیل:
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.HttpURLConnection;
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);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb=new StringBuilder();
reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line=reader.readLine())!=null){
sb.append(line+"\n");
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
if(reader!=null) reader.close();
}
}
}
کلاس Activity Main هم بصورت ذیل:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_coordi nator_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(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if(netinfo!=null && netinfo.isConnectedOrConnecting())
{
return true;
}
else return false;
}
(برنامه بدون خطا اجرا میشه و پیام Online بودن هم ظاهر میشه یعنی اینترنت اوکیه ولی فایل XML نشون داده نمیشه)
یه برنامه تو اندروید استودیو نوشتم برای خوندن اطلاعات فایل XML به ادرس ذیل:
http://services.hanselandpetal.com/feeds/flowers.xml
یه کلاس نوشتم به اسن HttpManager به شکل ذیل:
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.HttpURLConnection;
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);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb=new StringBuilder();
reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line=reader.readLine())!=null){
sb.append(line+"\n");
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
if(reader!=null) reader.close();
}
}
}
کلاس Activity Main هم بصورت ذیل:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_coordi nator_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(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if(netinfo!=null && netinfo.isConnectedOrConnecting())
{
return true;
}
else return false;
}