我正在做一個專案,在該專案中我創建了一個 API,我可以根據用戶輸入的位置從 twitter 獲取趨勢。現在我想在android中呼叫那個API。看起來像:

我想在這里展示 GET Trends:Get Trends Layout:
<RelativeLayout 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="@color/white"
tools:context=".trends">
<TextView
android:id="@ id/profiletitle"
android:layout_width="310dp"
android:layout_height="46dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:fontFamily="monospace"
android:text="Today's Trends"
android:textAlignment="center"
android:textColor="@color/Twit"
android:textSize="25sp"
android:textStyle="bold"
tools:layout_editor_absoluteX="48dp"
tools:layout_editor_absoluteY="29dp" />
<EditText
android:id="@ id/locationid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="105dp"
android:layout_marginTop="133dp"
android:ems="10"
android:hint="Enter your country"
android:inputType="textPersonName" />
<Button
android:id="@ id/getdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="216dp"
android:text="Get Trends" />
<FrameLayout
android:id="@ id/showtrends"
android:layout_width="360dp"
android:layout_height="492dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="310dp">
</FrameLayout>
</RelativeLayout>

我需要你的幫助來告訴我如何在 kotlin 中做到這一點。我已經搜索了很多次,但沒有找到可以將我的Country 引數從andriod 傳遞到 API然后獲取json 結果的解決方案。您的回答將不勝感激。
我要顯示的回應正文:

uj5u.com熱心網友回復:
請嘗試一次并根據您的要求制作 JSON 以及其他相關更改,例如每次單擊時清除 textview 等... -
檔案
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:fontFamily="monospace"
android:text="Today's Trends"
android:textAlignment="center"
android:textSize="25sp"
android:textStyle="bold" />
<EditText
android:id="@ id/locationid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Enter your country"
android:inputType="text" />
<Button
android:id="@ id/getData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Get Trends" />
<TextView
android:id="@ id/showData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:textSize="15sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
類檔案-
public class MainActivity4 extends AppCompatActivity implements View.OnClickListener {
Button getData;
EditText locationId;
TextView showData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
locationId = findViewById(R.id.locationid);
showData = findViewById(R.id.showData);
getData = findViewById(R.id.getData);
getData.setOnClickListener(this);
}
@SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.getData) {
String location = locationId.getText().toString().trim();
showData.setText("");
if (location.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please add Country", Toast.LENGTH_LONG).show();
return;
}
RequestQueue requestQueue;
requestQueue = Volley.newRequestQueue(getApplicationContext());
@SuppressLint("SetTextI18n") JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
"https://twtsentiment-fastapi.herokuapp.com/get_trend?loc=" location,
null, response -> {
Log.d("Response", response.toString());
try {
JSONArray jsonArray = response.getJSONArray("Trends");
for (int i = 0; i < jsonArray.length(); i ) {
showData.append("\n " jsonArray.optString(i) "\n ");
Log.d("TEXT", jsonArray.optString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> Log.d("myapp", "something went wrong"));
requestQueue.add(jsonObjectRequest);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476546.html
