java - Android Simple HTTP Request? -
i have code android application i'm trying create interact php website. have android.permission.internet permission activated , keeps creating toast says "error." instead of contents of website. here java file:
package com.http.request; import java.io.ioexception; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import android.app.activity; import android.os.bundle; import android.widget.toast; public class httprequestactivity extends activity { /** called when activity first created. */ private string dohttprequest(string url){ string results = "error"; try { httpclient hc = new defaulthttpclient(); httppost post = new httppost(url); httpresponse rp = hc.execute(post); if(rp.getstatusline().getstatuscode() == httpstatus.sc_ok) { results = entityutils.tostring(rp.getentity()); } }catch(ioexception e){ e.printstacktrace(); } return results; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); string results = dohttprequest("http://www.yahoo.com"); toast.maketext(getapplicationcontext(), results, toast.length_long).show(); } }
i check make sure that,
- if there exception being thrown, investigate causing ioexception
- your server potentially returning non-200 response code.
put in breakpoints , see whats happening there. bet on response code.
Comments
Post a Comment