解析下面的數據遇到問題時。在使用參數發送JSON對象時在JSON解析中獲取錯誤
"12-13 14:18:41.769: E/JSON Parser(17409): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject"
我想送一個像這樣的請求對象,也想打印請求對象同時發送: -
"objTimesheet" :
{
"ClassicLevel" : "1",
"CurrentLevel" : "2",
"UpdatedDate" : "5-12-13",
"Name":"Ankit",
"UpdatedTime": "20",
"Message":""
}
這是我的JSON解析器類: -
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
/* BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);*/
StringBuilder sb = new StringBuilder();
String line = null;
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
return jObj;
}
}
在我的活動中,我執行以下操作: -
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair(CLASSICLevel, "1"));
params1.add(new BasicNameValuePair(CURRENTLevel, "2"));
params1.add(new BasicNameValuePair(UPDATEDate, "345"));
params1.add(new BasicNameValuePair(NAME, "Nil"));
params1.add(new BasicNameValuePair(UPDATETIME, "10"));
json = jparser.makeHttpRequest(url_login, "POST", params1);
任何一個可以給我妥善解決這一提前發出上述請求,並得到response..Thanks
您可以登錄響應,並張貼 – Raghunandan
這將是輸出ISF sucees:---- InsertTimesheetItemResult =成功插入 –