import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.httpclientrequestforpost.utils.StreamTools;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity-->";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnHttpClientRequest(View view){
new Thread(){
@Override
public void run() {
}
}.start();
}
/**
* 使用HttpClient进行请求数据
* 请求方式:POST
*/
private void httpClientRequestForPost() {
try {
//1.打开浏览器
//BasicNameValuePair 键值对类型的
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("lng","121.538123"));
parameters.add(new BasicNameValuePair("lat","31.677132"));
HttpResponse response = httpClient.execute(httpPost);
//4.判断服务器是否应答了我们的请求
//getStatusLine() 得到状态行
//getStatusCode() 得到状态行里面的状态码
if (code == HttpStatus.SC_OK){
//得到服务给我们响应的内容
InputStream is = response.getEntity().getContent();
String json = StreamTools.readFromNetWork(is);
Log.e(TAG, "httpClientRequestForPost: "+json);
}
} catch (Exception e) {
e.printStackTrace();
}
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.httpclientrequestforpost.utils.StreamTools;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity-->";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnHttpClientRequest(View view){
new Thread(){
@Override
public void run() {
super.run();
//调用方法
}
}.start();
}
/**
* 使用HttpClient进行请求数据
* 请求方式:POST
*/
private void httpClientRequestForPost() {
try {
//1.打开浏览器
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://apis.juhe.cn/catering/query");
//BasicNameValuePair 键值对类型的
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("lng","121.538123"));
parameters.add(new BasicNameValuePair("lat","31.677132"));
parameters.add(new BasicNameValuePair("key","b1cc75a5744d69064bac2549993920f6"));
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse response = httpClient.execute(httpPost);
//4.判断服务器是否应答了我们的请求
//getStatusLine() 得到状态行
//getStatusCode() 得到状态行里面的状态码
int code = response.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK){
//得到服务给我们响应的内容
InputStream is = response.getEntity().getContent();
String json = StreamTools.readFromNetWork(is);
Log.e(TAG, "httpClientRequestForPost: "+json);
}
} catch (Exception e) {
e.printStackTrace();
}
}
需要在bulid添加
android{
useLibrary 'org.apache.http.legacy'
}