org.json.JSONException:值小于,java.lang.String类型的DOCTYPE不能转换到的JSONObject [英] org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
问题描述
下面我想用API键显示JSON内容。但我无法得到验证。
我收到错误的JSONObject:
org.json.JSONException:java.lang.String类型的值授权不能转换成的JSONObject
在我的Android应用程序,我只是通过API密钥和URL ID来获得在以下网址JSON响应。我展示使用JSON数组的JSON内容。
但是,如果我:
公共类AndroidAPiActivity延伸活动{
/ *
* FlickrQuery = FlickrQuery_url
* + FlickrQuery_per_page
* + FlickrQuery_nojsoncallback
* + FlickrQuery_format
* + FlickrQuery_tag + Q
* + FlickrQuery_key + FlickrApiKey
* /
字符串FlickrQuery_url =http://192.138.11.9/api/interests/;
串FlickrQuery_per_page =与&amp; per_page = 1;
串FlickrQuery_nojsoncallback =与&amp; nojsoncallback = 1;
字符串FlickrQuery_format =&放大器;格式= JSON;
字符串FlickrQuery_tag =&放大器;标签=;
串FlickrQuery_key =与&amp; API_KEY =;
//申请您的Flickr API:
// www.flickr.com/services/apps/create/apply/?
字符串FlickrApiKey =f65215602df8f8af;
的EditText searchText;
按钮searchButton;
TextView的textQueryResult,textJsonResult;
ImageView的imageFlickrPhoto;
位图bmFlickr;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
searchText =(EditText上)findViewById(R.id.searchtext);
searchButton =(按钮)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);
}
私人Button.OnClickListener searchButtonOnClickListener
=新Button.OnClickListener(){
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
。字符串searchQ = searchText.getText()的toString();
字符串信息搜索结果= QueryFlickr(searchQ);
textQueryResult.setText(信息搜索结果);
字符串jsonResult = ParseJSON(信息搜索结果);
textJsonResult.setText(jsonResult);
如果(bmFlickr!= NULL){
imageFlickrPhoto.setImageBitmap(bmFlickr);
}
}};
私人字符串QueryFlickr(串Q){
字符串QRESULT = NULL;
字符串的QString =
FlickrQuery_url
+ FlickrQuery_per_page
+ FlickrQuery_nojsoncallback
+ FlickrQuery_format
+ FlickrQuery_tag + Q
+ FlickrQuery_key + FlickrApiKey;
HttpClient的HttpClient的=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(QString的);
尝试 {
HttpEntity httpEntity = httpClient.execute(HTTPGET).getEntity();
如果(httpEntity!= NULL){
InputStream中的InputStream = httpEntity.getContent();
读者=新的InputStreamReader(InputStream的);
BufferedReader中的BufferedReader =新的BufferedReader(中);
StringBuilder的StringBuilder的=新的StringBuilder();
字符串stringReadLine = NULL;
而((stringReadLine = bufferedreader.readLine())!= NULL){
stringBuilder.append(stringReadLine +\ N);
}
QRESULT = stringBuilder.toString();
}
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回QRESULT;
}
私人字符串ParseJSON(JSON字符串){
字符串jResult = NULL;
bmFlickr = NULL;
字符串key_id;
字符串类;
串子类别;
字符串称号;
字符串icon_image;
尝试
{
的JSONObject的JSONObject =新的JSONObject(JSON);
JSONObject的Json_photos = JsonObject.getJSONObject(利益);
JSONArray JsonArray_photo = Json_photos.getJSONArray(利息);
//我们只有一张照片在此练习
的JSONObject FlickrPhoto = JsonArray_photo.getJSONObject(0);
key_id = FlickrPhoto.getString(row_key);
类别= FlickrPhoto.getString(类);
子类= FlickrPhoto.getString(子类别);
标题= FlickrPhoto.getString(标题);
jResult =\ñkey_id:+ key_id +\ N
+的范畴:+分类+\ N
+子类别:+子类别+\ N
+的称号:+标题+\ N的;
bmFlickr = LoadPhotoFromFlickr(key_id,类别,子类别,标题);
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回jResult;
}
私人位图LoadPhotoFromFlickr(
字符串key_id,串类,串子类别,
串题){
位图BM = NULL;
字符串icon_image = NULL;
//字符串FlickrPhotoPath =;
字符串FlickrPhotoPath =http://182.72.180.34/media/+ icon_image +.JPG;
URL FlickrPhotoUrl = NULL;
尝试 {
FlickrPhotoUrl =新的URL(FlickrPhotoPath);
HttpURLConnection的的HttpConnection =(HttpURLConnection类)FlickrPhotoUrl.openConnection();
httpConnection.setDoInput(真正的);
httpConnection.connect();
InputStream中的InputStream = httpConnection.getInputStream();
BM = BitmapFactory.de codeStream(InputStream的);
}赶上(MalformedURLException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回BM;
}
}
更新:
基于HTML的响应,我可以告诉你,这不是JSON。响应告诉我,你有不正确的URL为Web服务。
您需要检查你的网址。
额外的资讯/ previous答:
看起来简单的答案是正确的 - 你的结果是不是有效的JSON字符串。请参见 JSON.org 的网站,了解有关JSON的应该是什么样的细节。
查看 JSON解析器在线 - 我使用JSON工作时,发现它非常有用
奇怪的是,您要求的JSON,并没有返回得当 - 也许我错过了一些东西。
Here I want to display the JSON content using API key. But I am unable to get the authentication.
I am getting the error in 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.
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;
}
}
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.
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.org website for details on what JSON should look like.
Check out JSON Parser Online - I find its very useful when working with JSON.
It is strange that you are requesting JSON, and it is not returning it properly - perhaps I have missed something.
这篇关于org.json.JSONException:值小于,java.lang.String类型的DOCTYPE不能转换到的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!