Android 将 String 转换为 StringEntity 时值不正确
Android Incorrect value when converting String to StringEntity
我正在使用 HttpPost 向服务器发送一个字符串。
这个字符串是一个 JSONObject,我之前已经将其转换为 String
到目前为止一切正常......
必须使用内容类型 "application / json".
发送
要发送它,将我的String转换成StringEntity并添加到httpPost.setEntity (my_string) ...问题是服务器告诉我无法识别数据(已准备好接收转换为字符串的 JSONObject)
在我的代码中,我在转换为 StringEntity 之前使用日志来了解 String 的值,结果如下:
String: {"Gender":"male","User":"Pedro","Email":"ejemplo@ejemplo.com","Language":"English"}
但是,当我把这个String转成StringEntity的时候,Log的结果不是一样的,而是如下:
String: org.apache.http.entity.StringEntity@16a4bc74
为什么?
我不知道我做错了什么...
我搜索了一下,发现了很多例子,我觉得我做的是对的...我不明白错误。
为什么字符串在转换为 StringEntity 时没有保持值?
我试过很多例子,比如http://hmkcode.com/android-send-json-data-to-server/,还有很多。
这是我的代码。
非常感谢您的帮助。
你好。
public static JSONObject makeServiceCall(String url, JSONObject params) {
try {
Log.i("dmode", "LLegan los valores:" + " " + params);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
if (params != null) {
String conversion = params.toString();
Log.i("dmode", "JSONBoject to String" + " " + conversion);
StringEntity se;
se = new StringEntity(conversion);
Log.e("dmode", "String to StringEntity" + " " + se);
// Set HTTP parameters
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity(se);
}
String response = null;
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
try {
jObject = new JSONObject(response);
} catch (Exception e) {
}
Log.i("dmode", "Devolución" + jObject);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jObject;
}
尝试使用 URL 连接:
public JSONObject readJSONFromURL(String urlString, JSONObject param){
String response = null;
JSONObject jObject = null;
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("dmode", param.toString()));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
response = getStringFromInputStream(in);
jObject = new JSONObject(response);
} catch (UnsupportedEncodingException e) {
Log.e("ERROR", "UnsupportedEncodingException " + e.toString());
} catch (IOException e) {
Log.e("ERROR", "IOException " + e.toString());
} catch (JSONException e) {
Log.e("ERROR", "JsonException: " + e.toString());
}
return jObject;
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
private String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
我正在使用 HttpPost 向服务器发送一个字符串。 这个字符串是一个 JSONObject,我之前已经将其转换为 String 到目前为止一切正常...... 必须使用内容类型 "application / json".
发送要发送它,将我的String转换成StringEntity并添加到httpPost.setEntity (my_string) ...问题是服务器告诉我无法识别数据(已准备好接收转换为字符串的 JSONObject)
在我的代码中,我在转换为 StringEntity 之前使用日志来了解 String 的值,结果如下:
String: {"Gender":"male","User":"Pedro","Email":"ejemplo@ejemplo.com","Language":"English"}
但是,当我把这个String转成StringEntity的时候,Log的结果不是一样的,而是如下:
String: org.apache.http.entity.StringEntity@16a4bc74
为什么?
我不知道我做错了什么...
我搜索了一下,发现了很多例子,我觉得我做的是对的...我不明白错误。 为什么字符串在转换为 StringEntity 时没有保持值?
我试过很多例子,比如http://hmkcode.com/android-send-json-data-to-server/,还有很多。
这是我的代码。
非常感谢您的帮助。
你好。
public static JSONObject makeServiceCall(String url, JSONObject params) {
try {
Log.i("dmode", "LLegan los valores:" + " " + params);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
if (params != null) {
String conversion = params.toString();
Log.i("dmode", "JSONBoject to String" + " " + conversion);
StringEntity se;
se = new StringEntity(conversion);
Log.e("dmode", "String to StringEntity" + " " + se);
// Set HTTP parameters
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity(se);
}
String response = null;
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
try {
jObject = new JSONObject(response);
} catch (Exception e) {
}
Log.i("dmode", "Devolución" + jObject);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jObject;
}
尝试使用 URL 连接:
public JSONObject readJSONFromURL(String urlString, JSONObject param){
String response = null;
JSONObject jObject = null;
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("dmode", param.toString()));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
response = getStringFromInputStream(in);
jObject = new JSONObject(response);
} catch (UnsupportedEncodingException e) {
Log.e("ERROR", "UnsupportedEncodingException " + e.toString());
} catch (IOException e) {
Log.e("ERROR", "IOException " + e.toString());
} catch (JSONException e) {
Log.e("ERROR", "JsonException: " + e.toString());
}
return jObject;
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
private String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}