我想显示从newsapi到textview的重新调谐的json数据的标题。有正确的方法吗?返回每篇文章的标题,我知道这一点,因为在for循环Log.i(“ Titles,title)内将所需的标题输出到logcat。我只需要以某种方式将它们转换为textviews或任何允许用户执行以下操作的方法查看标题。亲切的问候, 罗布。
public class quotesPage extends AppCompatActivity {
String FactsUrl;
Button goButton;
TextView displayBox;
String [] array;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quotes_page);
goButton = findViewById(R.id.button1);
displayBox = findViewById(R.id.textView4);
goButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
FactsUrl = "https://newsapi.org/v2/top-headlines?country=gb&category=health&apiKey=2b3d6472849543e4a908f43cf5c77410";
//displayBox.setText("THIS IS JUST A TEST ");
new quotesPage.AsyncHttpTask().execute(FactsUrl);
}
});
}
public class AsyncHttpTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... urls)
{
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL (urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
String response = streamToString(urlConnection.getInputStream());
parseResult(response);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
String streamToString(InputStream stream) throws IOException
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String data;
String result = "";
while ((data = bufferedReader.readLine()) != null)
{
result += data;
}
if (null != stream)
{
stream.close();
}
return result;
}
public void parseResult(String result)
{
JSONObject response = null;
try {
response = new JSONObject(result);
JSONArray articles = response.optJSONArray("articles");
for (int i = 0; i < articles.length(); i++)
{
JSONObject article = articles.optJSONObject(i);
String title = article.optString("title");
Log.i("Titles", title);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
activity_quotes_page.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
tools:context=".quotesPage">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="445dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/baloo"
android:text="Click the GO! button to see uk health news headlines!"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginRight="130dp"
android:background="?attr/colorButtonNormal"
android:fontFamily="@font/baloo"
android:text="Go!"
android:textSize="24sp"
android:visibility="visible" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="344dp"
android:layout_height="62dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
app:srcCompat="@drawable/leftarrowlarge" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
app:srcCompat="@drawable/timerlarge" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_orange_dark"
app:srcCompat="@drawable/rightarrowlarge" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
id喜欢做的事情是在“ parseResult” for循环中做到这一点:
displayBox.append(displayBox + title);
答案 0 :(得分:0)
答案 1 :(得分:0)
我通常创建一个用于轻松解析和使用网络数据的模型。
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev)
{
ev.preventDefault();
var data = ev.dataTransfer.getData("text"); // ID of the incoming dragged element
if(ev.target.childElementCount==0)
{
ev.target.appendChild(document.getElementById(data));
for(var i=0;i<func_list[parseInt(data)].length;i++)
{
var exp=document.createElement("div");
exp.setAttribute("id", data.concat("exp"));
exp.setAttribute("ondrop","drop(event)");
exp.setAttribute("ondragover","allowDrop(event)");
exp.setAttribute("class","Expression_Area");
ev.target.appendChild(exp);
}
}
else
{
var item = ev.target;
while (item.firstChild)
{
item.removeChild(item.firstChild);
}
ev.target.appendChild(document.getElementById(data));
for(var i=0;i<func_list[parseInt(data)].length;i++)
{
var exp=document.createElement("div");
exp.setAttribute("id", data.concat("exp"));
exp.setAttribute("ondrop","drop(event)");
exp.setAttribute("ondragover","allowDrop(event)");
exp.setAttribute("class","Expression_Area");
ev.target.appendChild(exp);
}
}
}
现在根据需要显示列表视图或回收站视图。