在解析数据时面临以下问题。
"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"
我想发送这样的Request对象,也想在发送时打印请求对象:
"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);
任何人都可以为此给我适当的解决方案,以发送上述请求并获得答复。。
最佳答案
这是实际的json格式
{"countrylist":[{"id":"241","country":" India"}]}
检查您的json格式