我想检查给定的URL是否提供JSON内容。我怎么能做到这一点?我正在使用以下方法从URL读取JSON。我没有得到任何关于这个主题的答案。我需要帮助来实现这一目标。
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
resultOk=true;
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
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();
jsonString = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error",
"Error converting result " + e.toString());
}
} else {
resultOk=false;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jsonObjects = new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jsonObjects;
}
答案 0 :(得分:1)
您可以检查您的回复字符串。如果它以“{”或“[”字符开头,则表示响应采用json格式。
答案 1 :(得分:1)
try {
jsonObjects = new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
这本身应该足以测试你是否真的有JSON。
如果没有,则应该提出异常。只是检查你收到的字符串是否以“{”开头并不是真的有任何迹象: {这是无效的JSON ..}
答案 2 :(得分:1)
[已关闭]我通过更改并检查“resultOk”标志得到了正确的解决方案。在这里:
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
resultOk = true;
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
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();
jsonString = sb.toString();
jsonObjects = new JSONObject(jsonString);
} catch (IOException e) {
Log.e("Buffer Error",
"Error converting result " + e.toString());
} catch (JSONException e) {
resultOk = false;
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
} else {
resultOk = false;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObjects;
}
来自部分:
if (JSONObjectParser.resultOk){
//Show Success dialog with some good message
//And rest of the code....
} else{
//Show Error dialog with some error message
//And rest of the code....
}
最后这个对我来说很好。谢谢你...
答案 3 :(得分:0)
这是有效的json格式
{
"ECom": "",
"EDept": "01",
"EFlr": "GF",
"EComId": "",
"EDeptId": "01",
"EFlrId": "GF",
"EName": "EMPLOYEE 1",
"ECode": "00000001",
"VId": "201200000002",
"VName": "",
"VComp": "",
"VAddr": "",
"VMob": "",
"VEmail": "",
"VContact": "",
"VCat": "3",
"VPurpose": "1",
"VLaptop": "LAPTOP",
"VBel": "",
"VGender": "M",
"VRmk1": "",
"VRmk2": "",
"NOV": ""
}