我需要从php server获取listview json。我的api存在问题,这是我的api链接
http://staging.homeneedsonline.com/ws/ws_exe_newenq.php?exeId=2
在我的浏览器中,我得到了适当的回复。在android asyn任务中这个相同的url我得到错误响应。
{"success":"0","message":"New enquiry not found ."}
可能这将是无逻辑的问题,但我找不到错误。这个Api问题或应用程序问题?我做错了什么?
这是我的代码
protected JSONObject doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
UserFunctions userFunctions = new UserFunctions();
HashMap<String, String> user = userFunctions.getExinfo(EnquiryActivity.this);
String userid1 = user.get("exid");
System.out.println(userid1);
final String URL_LIST1 = "http://staging.homeneedsonline.com/ws/ws_exe_newenq.php?exeId=2";
System.out.println(URL_LIST1);
JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.getJSONFromUrl(URL_LIST1, get, params);
System.out.println(json);
getJSONFROmUrl方法
public JSONObject getJSONFromUrl(String url,String method, List<NameValuePair> params) {
// Making HTTP request
try {
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);
httpGet.addHeader("Cache-Control", "no-cache");
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);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
return null;
}
// 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 null;
}
// return JSON String
return jObj;
}
答案 0 :(得分:1)
你传递了一个params地图,你的getJSONfromUrl附加了一个“?”到您的URL后跟这些参数。但是,在您的特定情况下,参数是空的,并且您实际上已经在url字符串中提供了查询参数。 这意味着您实际获得的URL是 http://staging.homeneedsonline.com/ws/ws_exe_newenq.php?exeId=2?
请注意尾随问号。如果您在浏览器中打开此URL,则会收到相同的错误响应。
解决方案是
从URL中删除exeId参数,然后将其作为参数映射的一部分传递给getJSONfromUrl追加
完全停止传递参数并假设网址已经完成(在您的情况下)
在向网址添加任何查询之前,添加一项检查以查看param地图是否为空(这应该在任何情况下完成)
if (params.size() > 0) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
}
另请注意,您应使用String.equals()
而不是==来比较您的“GET”,“POST”等字符串。
答案 1 :(得分:0)
像这样更改您的JSONParser
班级GET
。
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);
httpGet.addHeader("Cache-Control", "no-cache");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
原因是不想要的空白&#34; params&#34;通过你的URL传递,没有必要 传递&#34; params&#34;如果没有必要。
如果&#34; params&#34;为null,它会像下面一样更改您的网址。
http://staging.homeneedsonline.com/ws/ws_exe_newenq.php?exeId=2?
此网址向您提供了错误回复。
因此,请避免追加&#34; params&#34;在网址中。