我使用此代码发送并从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();
}
但大多数代码都弃用了
不推荐使用NameValuePair类型
不推荐使用构造函数BasicNameValuePair(String,String)
不推荐使用HttpPost类型
不推荐使用HttpEntity类型
不推荐使用HttpResponse类型
...
上述方法的最佳替代方法是什么?
OkHttp或Apache HttpComponents - HttpClient还是有更好的方式?
谢谢