I've try to get data from my web /http://localhost/android_connection/getHttpGet.php
which follow the code from this site,http://www.thaicreate.com/mobile/android-httpget-httppost.html.
The result of "/http://localhost/android_connection/getHttpGet.php
" just after following this line :
Server Date/Time : 2014-06-04 04:28:49
I'm using Xampp for my localhost.
In android programming the code after >> HttpResponse response = client.execute(httpGet) doesn't run by catching log.e. Log.e("error1.1","Before try"); and Log.e("error1.2","Before HttpResponse"); only appear on my LogCat.
I don't know how to fix plz help me, by the way i'm a beginner in programming of using webserver via android application.
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final TextView result = (TextView) findViewById(R.id.result);
final Button btnGet = (Button) findViewById(R.id.btnGet);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String url = "http://localhost/android_connection/getHttpGet.php";
String resultServer = getHttpGet(url);
result.setText(resultServer);
}
});
}
public String getHttpGet(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
Log.e("error1.1","Before try");
try {
Log.e("error1.2", "Before HttpResponse");
HttpResponse response = client.execute(httpGet);
Log.e("error1.3", "End of HttpResponse");
StatusLine statusline = response.getStatusLine();
int statusCode = statusline.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
Log.e("error2","line : " + line);
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str.toString();
}
}