Android org.json.JSONException: 类型 java.lang.String 的值 <!DOCTYPE 不能转换为 JSONObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10876079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
提问by rajeshlawrance
Here I want to display the JSON content using API key. But I am unable to get the authentication.
这里我想使用 API 密钥显示 JSON 内容。但我无法获得身份验证。
I am getting the error in JsonObject:
我在 JsonObject 中收到错误消息:
org.json.JSONException: Value Authorization of type java.lang.String cannot be converted to JSONObject
In my android application, I just pass the API key and URL id to get the JSON response in the following URL. I display the JSON content using JSON array.
在我的 android 应用程序中,我只是通过 API 密钥和 URL id 来获取以下 URL 中的 JSON 响应。我使用 JSON 数组显示 JSON 内容。
But if I:
但如果我:
public class AndroidAPiActivity extends Activity {
/*
* FlickrQuery = FlickrQuery_url
* + FlickrQuery_per_page
* + FlickrQuery_nojsoncallback
* + FlickrQuery_format
* + FlickrQuery_tag + q
* + FlickrQuery_key + FlickrApiKey
*/
String FlickrQuery_url = "http://192.138.11.9/api/interests/";
String FlickrQuery_per_page = "&per_page=1";
String FlickrQuery_nojsoncallback = "&nojsoncallback=1";
String FlickrQuery_format = "&format=json";
String FlickrQuery_tag = "&tags=";
String FlickrQuery_key = "&api_key=";
// Apply your Flickr API:
// www.flickr.com/services/apps/create/apply/?
String FlickrApiKey = "f65215602df8f8af";
EditText searchText;
Button searchButton;
TextView textQueryResult, textJsonResult;
ImageView imageFlickrPhoto;
Bitmap bmFlickr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchText = (EditText)findViewById(R.id.searchtext);
searchButton = (Button)findViewById(R.id.searchbutton);
textQueryResult = (TextView)findViewById(R.id.queryresult);
textJsonResult = (TextView)findViewById(R.id.jsonresult);
imageFlickrPhoto = (ImageView)findViewById(R.id.flickrPhoto);
searchButton.setOnClickListener(searchButtonOnClickListener);
}
private Button.OnClickListener searchButtonOnClickListener
= new Button.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
String searchQ = searchText.getText().toString();
String searchResult = QueryFlickr(searchQ);
textQueryResult.setText(searchResult);
String jsonResult = ParseJSON(searchResult);
textJsonResult.setText(jsonResult);
if (bmFlickr != null){
imageFlickrPhoto.setImageBitmap(bmFlickr);
}
}};
private String QueryFlickr(String q){
String qResult = null;
String qString =
FlickrQuery_url
+ FlickrQuery_per_page
+ FlickrQuery_nojsoncallback
+ FlickrQuery_format
+ FlickrQuery_tag + q
+ FlickrQuery_key + FlickrApiKey;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(qString);
try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String stringReadLine = null;
while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}
qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return qResult;
}
private String ParseJSON(String json){
String jResult = null;
bmFlickr = null;
String key_id;
String category;
String subcategory;
String title;
String icon_image;
try
{
JSONObject JsonObject = new JSONObject(json);
JSONObject Json_photos = JsonObject.getJSONObject("interests");
JSONArray JsonArray_photo = Json_photos.getJSONArray("interest");
//We have only one photo in this exercise
JSONObject FlickrPhoto = JsonArray_photo.getJSONObject(0);
key_id = FlickrPhoto.getString("row_key");
category = FlickrPhoto.getString("category");
subcategory = FlickrPhoto.getString("subcategory");
title = FlickrPhoto.getString("title");
jResult = "\n key_id: " + key_id + "\n"
+ "category: " + category + "\n"
+ "subcategory: " + subcategory + "\n"
+ "title: " + title + "\n";
bmFlickr = LoadPhotoFromFlickr(key_id, category, subcategory,title);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jResult;
}
private Bitmap LoadPhotoFromFlickr(
String key_id, String category, String subcategory,
String title){
Bitmap bm= null;
String icon_image = null;
// String FlickrPhotoPath ="";
String FlickrPhotoPath ="http://182.72.180.34/media/"+icon_image+".jpg";
URL FlickrPhotoUrl = null;
try {
FlickrPhotoUrl = new URL(FlickrPhotoPath);
HttpURLConnection httpConnection = (HttpURLConnection) FlickrPhotoUrl.openConnection();
httpConnection.setDoInput(true);
httpConnection.connect();
InputStream inputStream = httpConnection.getInputStream();
bm = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bm;
}
}
回答by Richard Le Mesurier
Update:
更新:
Based on the HTML response, I can tell you that this is not JSON. The response tells me that you have the incorrect URL for your web service.
根据 HTML 响应,我可以告诉您这不是 JSON。响应告诉我您的 Web 服务的 URL 不正确。
You need to check your URL.
您需要检查您的网址。
Extra Info / Previous Answer:
额外信息/上一个答案:
It looks like the simple answer is the right one - your result is not a valid JSON string. See JSON.orgwebsite for details on what JSON should look like.
看起来简单的答案是正确的 - 您的结果不是有效的 JSON 字符串。有关JSON 应该是什么样子的详细信息,请参阅JSON.org网站。
Check out JSON Parser Online- I find its very useful when working with JSON.
查看JSON Parser Online- 我发现它在使用 JSON 时非常有用。
It is strange that you are requesting JSON, and it is not returning it properly - perhaps I have missed something.
奇怪的是,您正在请求 JSON,但它没有正确返回它 - 也许我错过了一些东西。
回答by Sravani
Yes, we get such kind of warning when the given URL is not valid.
是的,当给定的 URL 无效时,我们会收到此类警告。
Just check the URL once.
只需检查一次 URL。
回答by Shubham Kumar Gupta Ggps
May be this can help
可能这会有所帮助
https://teamtreehouse.com/community/solved-exception-cannot-convert-string-type-to-json-object
https://teamtreehouse.com/community/solved-exception-cannot-convert-string-type-to-json-object
Solved.
解决了。
It turns out the runtime error stretched back to the previous video.
事实证明,运行时错误可以追溯到上一个视频。
I was doing
我在做
JSONObject currently = new JSONObject("currently"); instead of
JSONObject 当前 = new JSONObject("currently"); 代替
JSONObject currently = forecast.getJSONObject("currently");
So my guess is Android thought I was trying to setup an entirely new JSON object instead of trying to retrieve information from an existing one! :) Now the console displays the time perfectly!
所以我的猜测是 Android 认为我正在尝试设置一个全新的 JSON 对象,而不是尝试从现有对象中检索信息!:) 现在控制台完美地显示了时间!
回答by Mahmoud Ayman
I've faced this issue too, I changed my Internet connection to another networkand it works.
我也遇到过这个问题,我将 Internet 连接更改为另一个网络,并且可以正常工作。
The problem was that ISPdoesn't accept http
access.
问题是ISP不接受http
访问。
Another solution you can open VPNand try again, and maybe it works...
您可以打开VPN并重试的另一种解决方案,也许它有效......
回答by Mr.India
Remove docType from API. and set content Type Application/json . (as text/html will not read as json . thus you were seeing the error.)
从 API 中删除 docType。并设置内容类型 Application/json 。(因为 text/html 不会读取为 json 。因此您看到了错误。)
回答by Ashish Tanna
I received the same "<!Doctype..."
error when working with Google Translate's json URLs. Then, I found this code somewhere and it worked :
"<!Doctype..."
在使用 Google Translate 的 json URL 时,我收到了同样的错误。然后,我在某处找到了这段代码,它起作用了:
BasicHttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout((HttpParams)basicHttpParams, (int)10000);
HttpConnectionParams.setSoTimeout((HttpParams)basicHttpParams, (int)10000);
HttpConnectionParams.setTcpNoDelay((HttpParams)basicHttpParams, (boolean)true);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient((HttpParams)basicHttpParams);
HttpGet httpGet = new HttpGet(url);
BasicResponseHandler basicResponseHandler = new BasicResponseHandler();
JSONObject json=null;
try {
json = new JSONObject((String)defaultHttpClient.execute((HttpUriRequest)httpGet, (ResponseHandler)basicResponseHandler));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}