如何在Android Studio中使用HTTP Post发送参数
我想使用HTTP Post方法发送两个以上的参数 这是我的ServiceHandler.java如何在Android Studio中使用HTTP Post发送参数,android,json,xml,Android,Json,Xml,我想使用HTTP Post方法发送两个以上的参数 这是我的ServiceHandler.java import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValue
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
现在如何发送参数。例如,我需要发送一个值为xyz的参数abc
谢谢试试看
HttpPost httpPost = new HttpPost(url);
// adding post params
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("abc", "xyz"));
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost
HttpPost-HttpPost=新的HttpPost(url);
//添加post参数
列表参数=新的ArrayList(2);
参数添加(新的BasicNameValuePair(“abc”、“xyz”);
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost
我想从我的活动而不是从ServiceHandler.java发送参数。有可能吗?所以试试列出params=new ArrayList();params.add(new BasicNameValuePair(“abc”,“xyz”);
sh.makeServiceCall(url,ServiceHandler.POST,params);你能把代码发送到Thungle吗。it@gmail.com.我会在这里查看更多!
HttpPost httpPost = new HttpPost(url);
// adding post params
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("abc", "xyz"));
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost