I have done the code below for getting a xml result, but its not showing any result. I was expecting to see the result in the TextView txtVwHttp-which i have in the mainActivities layout xml file. Any suggestion? i have given the internet permission in manifest.xml.
public class MainActivity extends Activity
{
TextView txtvw;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtvw = (TextView)findViewById(R.id.txtVwHttp);
//If connection doesnt work, even aftr givin permission for internet
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("api.androidhive.info/pizza/?format=xml");
HttpResponse response;
try
{ response = httpClient.execute(httpGet);
Log.i("Preda", response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
if(entity != null)
{ InputStream inStream = entity.getContent();
String result = convertStreamToString(inStream); // my defined method
txtvw.setText(result);
inStream.close();
} } catch(Exception exc){}
}
private static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{ while((line = reader.readLine()) != null)
{ sb.append(line + "\n"); }
}
catch(Exception exp)
{ exp.printStackTrace() ;}
finally { try{ is.close(); }
catch(IOException ioExp){ ioExp.printStackTrace(); }
}
return sb.toString();
} }