gpt4 book ai didi

java - 如何使用 echo php 到我的 android fragment

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:30 25 4
gpt4 key购买 nike

我试图将 MySQL 回显到连接到 RecyclerView 的 android fragment 到另一个布局 fragment 。但是当我开始尝试 AsyncRetrieve 时,android 为 AsyncRetrieve 打开了一个新类,并在其中实现了 execute 我尝试进行更改,就好像 AsyncRetrieve 类是一个 Activity ,但我失败了,所以如果有人有经验的话在此请推荐一个解决方案

我将 inflater 更改为 View v =

     public View onCreateView(LayoutInflater inflater, ViewGroup 
container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_redirect, container,
false);

new AsyncRetrieve().execute();
return v;
<小时/>

然后我尝试编写这个代码

<小时/>
private class AsyncRetrieve extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
HttpURLConnection conn;
URL url = null;

//this method will interact with UI, here display loading message
@Override
protected void onPreExecute() {
super.onPreExecute();

pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();

}

// This method does not interact with UI, You need to pass result to onPostExecute to display
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://192.168.1.7/example.php");

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {

// Setup HttpURLConnection class to send and receive data from php
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");

// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}

try {

int response_code = conn.getResponseCode();

// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {

// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
result.append(line);
}

// Pass data to onPostExecute method
return (result.toString());

} else {

return ("unsuccessful");
}

} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}


}

// this method will interact with UI, display result sent from doInBackground method
@Override
protected void onPostExecute(String result) {

pdLoading.dismiss();
if(result.equals("Success! This message is from PHP")) {
textPHP.setText(result.toString());
}else{
// you to understand error returned from doInBackground method
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}

}

}

}

最佳答案

你收到 PHP 的回复了吗?如果您收到,请使用以下方法在日志中打印该响应。

    protected void onPostExecute(String result) {
}

然后绑定(bind)到 RecyclerView。

关于java - 如何使用 echo php 到我的 android fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55467639/

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