![](/img/trans.png)
[英]Httppost not working - Getting a html response instead of JSON in Android
[英]Getting HTML response instead of Json response
我正在獲取HTML響應而不是JSON響應。 我正在使用以下代碼,並且收到bf.readLine()的HTML響應。 以下代碼是否存在任何問題,或者此API存在問題?
String uri = "http://192.168.77.6/Ivr_ABN_API/?id=" + mobile;
URL url;
Gson json = null;
try {
url = new URL(uri);
json = new Gson();
HttpURLConnection connection;
access_token = db.getAccessTokenFromDB();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
System.out.println("URL:" + uri);
connection.setRequestProperty("Content-Type", "application/json");
int status = connection.getResponseCode();
resCode = Integer.toString(status);
System.out.println("status is " + status);
InputStream in = connection.getInputStream();
System.out.println("inputStreamer " + in);
BufferedReader bf = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
System.out.println("bf.readLine() - " + bf.readLine());
while ((output = bf.readLine()) != null) {
JSONObject obj = new JSONObject(output);
System.out.println("output is " + output);
resCode = obj.getString("resCode");
resDesc = obj.getString("COUNT");
}
也許嘗試發送標頭Accept:application / json
如果這不起作用,請查看API文檔,看看是否還有其他內容需要發送以返回json。
對於java
將請求屬性設置為以下內容:
con.setRequestProperty("Accept","application/json")
它將解決您面臨的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.