Android:从php发送/获取数据的最佳方式
我使用此代码从php发送和获取数据Android:从php发送/获取数据的最佳方式,android,httpclient,Android,Httpclient,我使用此代码从php发送和获取数据 public void SendGetData(){ class GetDataJSON extends AsyncTask<String, Void, String>{ private ProgressDialog pDialog; private InputStream is = null; private String url = "http://***/SendGet.php";
public void SendGetData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
private ProgressDialog pDialog;
private InputStream is = null;
private String url = "http://***/SendGet.php";
private String page_output = "";
@Override
protected String doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Name", ed_name.getText().toString()));
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
page_output = sb.toString();
Log.i("LOG", "page_output --> " + page_output); ///<--------------------------|
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return page_output;
}
@Override
protected void onPostExecute(String result){
Log.i("LOG", " onPostExecute -> " + result );
myJSON=result;
Log.i("LOG", "myJSON" + myJSON);
}
}
GetDataJSON g = new GetDataJSON();
Log.i("LOG", " GetDataJSON " );
g.execute();
}
public void SendGetData(){
类GetDataJSON扩展了AsyncTask{
私人对话;
私有InputStream为空;
私有字符串url=“http://***/SendGet.php”;
私有字符串页_输出=”;
@凌驾
受保护的字符串doInBackground(字符串…args){
试一试{
//建筑参数
List params=new ArrayList();
add(新的BasicNameValuePair(“Name”,ed_Name.getText().toString());
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“UTF-8”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
page_output=sb.toString();
Log.i(“日志”、“页面输出-->”+页面输出)///