NetworkUtilities.java
公共类NetworkUtilities{
private static final String TAG = NetworkUtilities.class.getSimpleName();
public static URL createUrl(String stringUrl){
URL url = null;
try{
url = new URL(stringUrl);
}catch (MalformedURLException e){
Log.v(TAG, "Problem building the Url");
}
return url;
}
public static String httpRequest(URL url) throws IOException{
String jsonResponse = "";
if(url ==null){
Log.v(TAG, "Url is null");
return jsonResponse;
}
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
try{
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
if(httpURLConnection.getResponseCode() == 200){
inputStream = httpURLConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
}
else{
Log.e(TAG, "Error response code" + httpURLConnection.getResponseCode());
}
}catch (IOException e){
Log.v(TAG, "Problem retrieving the json result", e);
}finally {
if(httpURLConnection != null){
httpURLConnection.disconnect();
}
if(inputStream != null){
inputStream.close();
}
}
return jsonResponse;
}
private static String readFromStream(InputStream inputStream) throws IOException{
StringBuilder output = new StringBuilder();
if(inputStream != null){
InputStreamReader in = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader bf = new BufferedReader(in);
String line = bf.readLine();
while(line != null){
output.append(line);
line = bf.readLine();
}
}
return output.toString();
}
public static List<String> extractFromJson(String jsonResponse){
if(TextUtils.isEmpty(jsonResponse)){
return null;
}
List<String> newsStories = new ArrayList<>();
try{
JSONObject baseObj = new JSONObject(jsonResponse);
JSONArray articlesArray = baseObj.getJSONArray("data");
for(int i=0;i<articlesArray.length();i++){
JSONObject currentArticle = articlesArray.getJSONObject(i);
JSONObject source = currentArticle.getJSONObject("source");
String sourceName = source.getString("name");
String title = currentArticle.getString("title");
String description = currentArticle.getString("description");
String newsStory = "Source" + sourceName + "/n" + title + "/n" + description;
newsStories.add(newsStory);
}
}catch (JSONException e){
Log.e(TAG, " Problem parsing the json string", e);
}
return newsStories;
}
NewsAdapter.java
公共类NewsAdapter扩展RecyclerView.Adapter
private Context mContext;
private List<String> mNewsArticles;
NewsAdapter(Context context){
mContext = context;
}
@NonNull
@Override
public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(mContext)
.inflate(R.layout.news_list_item,parent, false);
view.setFocusable(true);
return new NewsViewHolder(view);
}
@Override
public void onBindViewHolder( NewsViewHolder holder, int position) {
String currentArticle = mNewsArticles.get(position);
holder.mTextView.setText(currentArticle);
}
@Override
public int getItemCount() {
if(mNewsArticles != null){
return mNewsArticles.size();
}
return 0;
}
public class NewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
final TextView mTextView;
public NewsViewHolder(@NonNull View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "position :" + getLayoutPosition(), Toast.LENGTH_SHORT).show();
}
}
public void setNewsData(List<String> newsData){
mNewsArticles = newsData;
notifyDataSetChanged();
}
mainactivity.java
公共类MainActivity扩展AppCompatActivity{
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView mRecyclerView;
private NewsAdapter mNewsAdapter;
private static final String BASE_URL = "https://newsapi.org/v2/top-headlines?country=us&apiKey=13f428d687714c33a24f34ad6c5***87";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(
new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
mRecyclerView.setHasFixedSize(true);
mNewsAdapter = new NewsAdapter(this);
mRecyclerView.setAdapter(mNewsAdapter);
new FetchNewsArticle().execute(BASE_URL);
}
public class FetchNewsArticle extends AsyncTask<String, Void, List<String>>{
@Override
protected List<String> doInBackground(String... strings) {
String stringUrl = strings[0];
URL url = NetworkUtilities.createUrl(stringUrl);
String json = "";
try{
json = NetworkUtilities.httpRequest(url);
List<String> articles = NetworkUtilities.extractFromJson(json);
return articles;
}catch (Exception e){
e.printStackTrace();
Log.v(TAG, "Problem retrieving data");
return null;
}
}
@Override
protected void onPostExecute(List<String> strings) {
if(strings != null){
mNewsAdapter.setNewsData(strings);
}
}
}
错误
w/zygote:无法打开libbeluga.so:dlopen失败:找不到库“libbeluga.so”。d/networksecurityconfig:未指定网络安全配置,使用平台默认值d/networksecurityconfig:未指定网络安全配置,使用平台默认值w/xample.newsfee:访问隐藏方法landroid/view/view;>ComputeFitSystemWindows(Landroid/Graphics/rect;Landroid/Graphics/rect;)Z(greylist,reflection,allowed)w/xample.newsFeed:访问隐藏方法Landroid/View/ViewGroup;->MakeOptionalFitsSystemWindows()V(greylist,reflection,allowed)com.example.newsFeed V/NetworkUtilities:检索json结果的问题
java.io.ioException:com.android.okhttp.httpandler$cleartexturlfilter.checkurlpermitt(httpandler.java:127)在com.android.okhttp.internal.huc.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:462)在com.android.okhttp.internal.huc.httpurlconnectionimpl.connect(httpurlconnectionimpl.java:131)在com.example.newsfeed.utils.networkutilities.httprequest(thread.java:923)2021-03-11 22:53:28.124 939 1-9391/com.example.newsfeed w/looper:PerfMonitor looperActivity:package=com.example.newsfeed/.mainactivity time=1ms latency=447ms running=2ms procstate=2 ClientTransaction{callbacks=[android.app.servertransaction.topresumedactivitychangeitem]}historymsgcount=4(msgindex=3 wall=87ms seq=3le.newsfeed I/adrenogles-0:驱动程序路径:/vendor/lib64/egl/libglesv2_adreno.so 2021-03-11 22:53:28.213 939 1-9429/com.example.newsfeed I/adrenogles-0:pfp:0x016EE190,ME:0x00000000 2021-03-11 22:53:28.253 939 1-9429/com.example.newsfeed E/lb:无法打开文件:没有这样的文件或目录
错误似乎是:java.io.IOException:不允许到api.mediastack.com的明文HTTP通信
。
从Android 9开始,默认禁用明文http通信。
请查看官方的Android文档,以及这个问题以获取更多的信息。
我们正尝试通过以下http请求获取Office 365组元数据:https://graph.microsoft.com/v1.0/groups/?$select=description、displayName、groupTypes、mail、mailEnabled、mailNickname、onpremissieslastsyncdatetime、onpremissiessecurityident
//回收器和视图 //调用提取数据方法extractData(); //提取数据
问题内容: 我正在尝试使用ESAPI.jar为我的Web应用程序提供安全性。基本上我刚刚开始使用ESAPI.jar。但是问题是我什至无法使用ESAPI运行简单的程序。小代码段是: 我收到此错误: 我尝试将3个ESAPI属性文件复制到我的源文件夹中,并在构建路径上进行配置,但是仍然没有成功。我尝试了许多排列和组合都无济于事。 请指导我。 属性文件的内容为: 问题答案: ESAPI.propertie
我试图从使用vba的SQL查询中获取一些数据,但当我尝试运行代码时,它会给我一个类型不匹配错误。有人能帮忙吗
我有一个Google App Engine应用程序,其中数据存储在Google Cloud Datastore中。我想使用Dataflow将部分数据放入BigQuery,但我想我应该从从Datastore获取一些信息并将其写入Google Cloud Storage开始。我的代码如下所示: 但是,当我尝试运行它时,我在进行Datastore查询时收到403个错误: 我使用Google Cloud
错误为:无法获取项目:在“https://dynamodb.us-east-1.amazonaws.com”上执行“GetItem”时出错;AWS HTTP错误:客户端错误:导致响应:{"__type":"com.amazon.coral.validate#ValidationException","消息":"提供的键元素与架构不匹配"(截断...)ValidationException(客户端)