***********************************
test3.php
***********************************
<?php
?>
**********************************************************************
GetPhpServerMessageDemoActivity.java
**********************************************************************
public class GetPhpServerMessageDemoActivity extends Activity {
/** Called when the activity is first created. */
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.text_view);
String msg = stringQuery("http://localhost/androidapp/android_connect/test3.php");
textView.setText("Server message is "+msg);
}
public String stringQuery(String url){
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = httpclient.execute(method);
/*HttpEntity entity = response.getEntity();*/
if(response != null){
return response.getStatusLine().toString();
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
}
因為我們只要測試是否連線正常 回傳 200 為正常
只要有產生 php 的檔案就可以了!!
Android 4.0 ↑請看 :
Recognizer + Android 4.0↑ HttpClient 實作
沒有留言:
張貼留言