gpt4 book ai didi

java - Android:POST请求,显示特定文本

转载 作者:行者123 更新时间:2023-12-02 13:39:48 26 4
gpt4 key购买 nike

好吧,我对 POST 和 GET 请求等概念很陌生。所以,如果这是一个愚蠢的问题,我深表歉意。

我在这个 TvrdjavaFragment.java 中有一个异步任务,它获取并显示 JSON 对象中的所有内容作为注释(toast)。例如,我只需要 temperatura 并将其显示为注释,而不是所有内容。我的问题是如何从 JSON 对象获取特定内容

这是我的 TvrdjavaFragment.java 文件:

public class TvrdjavaFragment extends Fragment {

Button btnIdinaperiod;
TextView pokaziServer;
String rezultat = "";
String strURL = "http://MYLINK";

public TvrdjavaFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_tvrdjava, container, false);
// Inflate the layout for this fragment

pokaziServer = (TextView) view.findViewById(R.id.testServer);
//int i = Integer.parseInt(pokaziServer.getText().toString());

//Log.d("TAG", "TestLogIvan");
new NabaviServer().execute();

btnIdinaperiod = (Button) view.findViewById(R.id.buttonIdinaperiod);

btnIdinaperiod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PeriodFragment periodFragment = new PeriodFragment();
FragmentTransaction periodFragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
periodFragmentTransaction.replace(R.id.frame, periodFragment);
periodFragmentTransaction.addToBackStack(null); //Kada pretisne BACK, vrati se nazad
periodFragmentTransaction.commit();
}
});

return view;
}

public class NabaviServer extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String s) {
//super.onPostExecute(s);

Toast.makeText(getActivity(), "Izlazak je: " + rezultat,Toast.LENGTH_LONG).show();
Log.i("IVANTAG", rezultat);
}

@Override
protected String doInBackground(String... params) {

try{
URL url = new URL(strURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.connect();

BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream())); //??s

String value = bf.readLine();
System.out.println("test " + value);
rezultat = value;


}
catch(Exception e)
{
System.out.println(e);
}

return null;
}
}

}

这就是我的 JSON 对象的样子:

[
{
"id": 1,
"vPritisak": "1",
"vVazduha": "0",
"nVisina": "0",
"temperatura": "0",
"metan": "0",
"uDioksid": "1",
"lokacija": "Kicevo",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"vPritisak": "0",
"vVazduha": "2",
"nVisina": "0",
"temperatura": "0",
"metan": "0",
"uDioksid": "0",
"lokacija": "Cair",
"created_at": null,
"updated_at": null
},
etc...

编辑:当我尝试添加 JSONObject jObj = new JSONObject(s); 时,我只收到错误“Unhandled exception: org.json.JSONException ”。当我尝试添加 Try - Catch (就像它所建议的那样)时,应用程序崩溃了:

       @Override
protected void onPostExecute(String s) {
//super.onPostExecute(s);

try {
JSONObject jObj = new JSONObject(s);
String temperatura = jObj.getString("temperatura");//get ur temperatura here

//try to toast it out,to see the value
Toast.makeText(getActivity(), "Izlazak je: " + temperatura,Toast.LENGTH_LONG).show();

} catch (JSONException e) {
e.printStackTrace();
}


Toast.makeText(getActivity(), "Izlazak je: " + rezultat,Toast.LENGTH_LONG).show();
Log.i("IVANTAG", rezultat);
}

最佳答案

您的响应是 JSONArray

JSONArray array_data = new JSONArray(value);
JSONObject object_data = array_data.getJSONObject(0); // 0, 1, 2 index of array

您可以从对象中获取任何数据

object_data.getString("temperatura");

关于java - Android:POST请求,显示特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42790890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com