在Android中进行JSON解析,因为我想在http URL上发布以下格式的数据,
webdata={"email":"test@test.com","password":"123456"}
我如何在 http url 上使用 httppost 为这个 JSON 发布这些数据?
首先,您可以准备您的 json 格式数据,然后根据您的要求将其作为您的请求发送 GET 或 POST 这需要使用 Asynctask 完成
JSONObject jObject = new JSONObject();
try{
jObject.put("email",urvalue);
jObject.put("password",urvalue);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("webdata", jObject.toString()));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}catch(Exception e){}
使用以下方法生成 json 对象
private JSONObject getConvertedinJson(String email, String password) {
JSONObject object = new JSONObject();
try {
object.put("email", email);
object.put("password", password);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
之后使用以下方法在 url 上发布 json 对象
public JSONObject getJSONFromUrl(String url, JSONObject jObj) {
// Making HTTP request
try {
// Default Http Client
DefaultHttpClient httpClient = new DefaultHttpClient();
// Http Post Header
HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(jObj.toString());
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// Execute Http Post Request
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();
}
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*/
try {
// Getting Server Response
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
// Reading Server Response
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
Log.e("JSON Parser", jObj.toString());
return jObj;
}
现在使用以下语句获取 json 响应。
JSONObject jsonObject = getJSONFromUrl(
"www.url.com/api",
getConvertedinJson("test@test.com",
"123456"));
现在您可以根据需要轻松反序列化 jsonObject。
注意:您的帖子数据必须是 Json 对象。正如我所见 webdata={"email":"test@test.com","password":"123456"} 不是 jsonObject 这里 json Object 只是 {"email":"test@test.com","password ":"123456"}
使用StringEntity
类。
StringEntity entity = new StringEntity("Your JSON String",HTTP.UTF_8);
然后将其添加到您的HttpPost
httpPost.setEntity(entity);