Java 以JSONObject作为值的Android BasicNameValuePair
我正在尝试向本地服务器发送http POST请求 代码如下:Java 以JSONObject作为值的Android BasicNameValuePair,java,android,json,http,http-post,Java,Android,Json,Http,Http Post,我正在尝试向本地服务器发送http POST请求 代码如下: public JSONObject makeHttpRequest (String url, String method, List<NameValuePair> params) { try { if (method == "POST") { DefaultHttpClient httpClient = new DefaultHttpClient();
public JSONObject makeHttpRequest (String url, String method, List<NameValuePair> params) {
try {
if (method == "POST") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
httpStatusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpStatusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
jsonString = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
try {
jsonObject = new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data" + e.toString());
}
return jsonObject;
}
公共JSONObject makeHttpRequest(字符串url、字符串方法、列表参数){
试一试{
如果(方法==“POST”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
httpStatusCode=httpResponse.getStatusLine().getStatusCode();
HttpEntity HttpEntity=httpResponse.getEntity();
inputStream=httpEntity.getContent();
}else if(方法==“GET”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
httpStatusCode=httpResponse.getStatusLine().getStatusCode();
HttpEntity HttpEntity=httpResponse.getEntity();
inputStream=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
inputStream.close();
jsonString=sb.toString();
}捕获(例外e){
e、 printStackTrace();
}
试一试{
jsonObject=新的jsonObject(jsonString);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
返回jsonObject;
}
对于param,我想创建一个值对{“user”:SOMEJSONObject},但是当前的http POST只接受NameValuePair,它只接受字符串作为值 改为创建字符串实体:
httpPost.setEntity(new StringEntity("some string"));
你能解释一下实体是什么吗?“some string”是json字符串吗?