json字符串未在Android设备

时间:2015-09-04 07:15:59

标签: json string parsing jsonobject

我遇到了一个关于获取JObject的问题,我的应用程序正在模拟器中运行,并且它无法在设备中运行。因为字符串没有正确转换为JObject。

E/JSON 1111﹕ parsing data {"success":1,"projects":[{"thumbimg":"http:\/\/spvdigi.com\/diyaphp\/images\/lab.png","duration":"3 mins","updated_at":"0000-00-00 00:00:00","keywords":"law,white sheet,pen,pencil,mini bulbs,","materials":"pen,pencil,ball,worksheet","description":"about the law bla bla","subject":"science","target":"k2","created_at":"2015-08-14 14:24:54","pid":"1","rating":"4","procedure":"procedure about this project"},{"thumbimg":"http:\/\/spvdigi.com\/diyaphp\/images\/lab.png","duration":"3 mins","updated_at":"0000-00-00 00:00:00","keywords":"pen,pencil,color pencil,chart,sticks","materials":"pen,pencil,color pencil,chart,sticks,etc","description":"about description of the project","subject":"science","target":"k3","created_at":"2015-08-14 14:25:16",
"pid":"2","rating":"3","procedure":"about description of the procedure"},{"thumbimg":"http:\/\/spvdigi.com\/diyaphp\/images\/lab.png","duration":"5 mins","updated_at":"0000-00-00 00:00:00","keywords":"pen,pencil,wire,chart,sticks","materials":"pen,pencil,color pencil,chart,sticks,eraser","description":"about description of the project","subject":"english","target":"k4","created_at":"2015-08-14 14:25:21",
....
"pid":"8","rating":"3","procedure":"about description of the procedure"}]}


the above is the output of the jsonObject, the below is the correct result showing in emulator

{"projects":[{"pid":"1","target":"k2","subject":"science","description":"about the law bla bla","duration":"3 mins","materials":"pen,pencil,ball,worksheet","procedure":"procedure about this project","keywords":"law,white sheet,pen,pencil,mini bulbs,","thumbimg":"http:\/\/spvdigi.com\/diyaphp\/images\/lab.png","rating":"4","created_at":"2015-08-14 14:24:54","updated_at":"0000-00-00 00:00:00"},{"pid":"2","target":"k3","subject":"science","description":"about description of the project","duration":"3 mins","materials":"pen,pencil,color pencil,chart,sticks,etc","procedure":"about description of the procedure","keywords":"pen,pencil,color pencil,chart,sticks","thumbimg":"http:\/\/spvdigi.com\/diyaphp\/images\/lab.png","rating":"3","created_at":"2015-08-14 14:25:16","updated_at":"0000-00-00 00:00:00"},
...
{"pid":"8","target":"k3","subject":"english","description":"to learn english tenses","duration":"3 mins","materials":"white sheet,pen,pencil,eraser","procedure":"about description of the procedure","keywords":"english,grammer,tenses","thumbimg":"http:\/\/spvdigi.com\/diyaphp\/images\/lab.png","rating":"3","created_at":"2015-08-18 06:45:28","updated_at":"0000-00-00 00:00:00"}],"success":1}


this is my jsonparse class code:


public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
static String jboolean = "0";
static JSONArray jarr =null;


// 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
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000 + 12000);
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type","application/json");
httpPost.setParams(httpParameters);
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();
Log.e("Buffer 11", "Error converting result " + e.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("Buffer 22", "Error converting result " + e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("Buffer 33", "Error converting result " + e.toString());
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
//Log.e("JSON 1111 ", "parsing data " + json.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);

Log.e("JSON 1111 ", "parsing data " + jObj.toString());
} catch (JSONException e) {
Log.e("JSON Parser44", "Error parsing data " + e.toString());
}

return jObj;

/// This JObj is not properly getting the json string???
// return JSON String
}
}

上述代码有什么问题吗?我正面临着获取JObject的问题,我的应用程序正在模拟器中运行,而且它无法在设备中运行。因为字符串没有正确转换为JObject。

0 个答案:

没有答案