这里我想使用API密钥显示JSON内容。但我无法获得身份验证。
我在JsonObject中收到错误:
org.json.JSONException: Value Authorization of type java.lang.String cannot be converted to JSONObject
在我的Android应用程序中,我只是传递API密钥和URL id以在以下URL中获取JSON响应。我使用JSON数组显示JSON内容。
但如果我:
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;
}
}
答案 0 :(得分:14)
基于HTML响应,我可以告诉你这不是JSON。该回复告诉我您的网络服务的网址不正确。
您需要检查自己的网址。
看起来简单的答案是正确的 - 您的结果不是有效的JSON字符串。有关JSON应该是什么样的详细信息,请参阅JSON.org网站。
查看JSON Parser Online - 我发现它在使用JSON时非常有用。
很奇怪你正在请求JSON,而且它没有正确地返回它 - 也许我错过了一些东西。
答案 1 :(得分:3)
是的,当给定的网址无效时,我们会收到此类警告。
只需检查一次网址。
答案 2 :(得分:0)
从API中删除docType。并设置内容类型Application / json。 (因为text / html不会读作json。因此你看到了错误。)
答案 3 :(得分:0)
使用Google翻译的json网址时,我收到了同样的"<!Doctype..."
错误。然后,我在某个地方找到了这个代码并且它有效:
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();
}
答案 4 :(得分:0)
可能对您有帮助
https://teamtreehouse.com/community/solved-exception-cannot-convert-string-type-to-json-object
已解决。
原来,运行时错误还可以追溯到上一个视频。
我在做
JSONObject当前=新的JSONObject(“ currently”); 代替
JSONObject currently = forecast.getJSONObject("currently");
所以我的猜测是Android认为我正在尝试设置一个全新的JSON对象,而不是尝试从现有对象中检索信息! :)现在控制台完美显示了时间!
答案 5 :(得分:0)
我也遇到了这个问题,我将我的Internet连接更改为另一个网络,并且可以正常工作。
问题是 ISP 不接受http
访问。
另一种解决方案,您可以打开 VPN ,然后重试,也许可行...