我正在开发一个从php服务器检索数据的应用程序。我从php服务器收到一个json数组。当我在Android模拟器中运行apk时,它工作得很好,但是当我在真正的Android手机上运行这个apk时出现问题。检索数据需要太多时间。我搜索了很多资源,但我无法解决这个问题。请任何人给我一些解决这个问题的提示。提前谢谢。
CostForSecretCloseMem类
public class CostForSecretCloseMem extends AppCompatActivity
{
private ListView listView;
private RelativeLayout emptyView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.costs_layout);
initComponent();
}
@SuppressLint("SetTextI18n")
private void initComponent()
{
emptyView = findViewById(R.id.empty_view);
SharedPreferenceData sharedPreferenceData = new SharedPreferenceData(this);
CheckInternetIsOn internetIsOn = new CheckInternetIsOn(this);
listView = findViewById(R.id.list);
if(internetIsOn.isOnline())
{
try {
String DATA = URLEncoder.encode("userName", "UTF-8") + "=" + URLEncoder.encode(sharedPreferenceData.getCurrentUserName(), "UTF-8");
//importantData.getAllShoppingCost(getResources().getString(R.string.shoppingCost), DATA,infoInterfaces);
TestBackground testBackground = new TestBackground(this);
testBackground.setOnResultListener(infoInterfaces);
testBackground.execute(getResources().getString(R.string.shoppingCost),DATA);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
//process shopping only_show_cost json data
private void processJsonData(String result)
{
List<CostModel> costList = new ArrayList<>();
int count=0;
String name,taka,date,id;
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.optJSONArray("costList");
while (count<jsonArray.length())
{
JSONObject jObject = jsonArray.getJSONObject(count);
id = jObject.getString("id");
name = jObject.getString("name");
taka = jObject.getString("taka");
date = jObject.getString("date");
costList.add(new CostModel(id,name,taka,date,""));
count++;
}
if(costList.isEmpty())
listView.setEmptyView(emptyView);
else
emptyView.setVisibility(View.INVISIBLE);
CostAdapter adapter = new CostAdapter(this, costList);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
//get all shopping list
OnAsyncTask infoInterfaces = new OnAsyncTask() {
@Override
public void onSuccess(final String result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(result!=null)
processJsonData(result);
}
});
}
};
}
的AsyncTask
public class TestBackground extends AsyncTask<String,Void,String>
{
private Context context;
private OnAsyncTask onAsyncTaskInterface;
public TestBackground(Context context)
{
this.context = context;
}
//success result
public void setOnResultListener(OnAsyncTask onAsyncResult) {
if (onAsyncResult != null) {
this.onAsyncTaskInterface = onAsyncResult;
}
}
@Override
public String doInBackground(String... params) {
String result = "";
String fileName = params[0];
String postData = params[1];
try {
URL url = new URL(fileName);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
OutputStream outputStream = httpURLConnection.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(postData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String line;
while ((line= bufferedReader.readLine())!=null)
result = line;
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
onAsyncTaskInterface.onSuccess(result);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e)
{
e.printStackTrace();
}
return result.toString();
}
public interface OnAsyncTask {
void onSuccess(String message);
}
}
php code
<?php
require"connection.php";
require"getSession.php";
$userName = $_POST["userName"];
$session = new Sessions();
$month = $session->session($userName);
$sql = "SELECT GroupID FROM member_info WHERE UserName LIKE '$userName';";
$result = mysqli_query($conn,$sql);
if($result->num_rows>0)
{
while($row=mysqli_fetch_array($result))
{
if($row["GroupID"]!="Null")
getCostList($row["GroupID"],$month);
}
}
function getCostList($value,$month)
{
require"connection.php";
$sql = "SELECT * FROM cost WHERE groupName LIKE '$value' and cMonth LIKE '$month';";
$result = mysqli_query($conn,$sql);
$response = array();
if($result->num_rows>0)
{
while($row=mysqli_fetch_array($result))
array_push($response,array("id"=>$row[0],"name"=>$row[1],"taka"=>$row[4],"date"=>$row[5]));
echo json_encode(array("costList"=>$response));
}
}
$conn->close();
?>
答案 0 :(得分:0)
你的代码中犯了很多错误。
如果你采用上述方法,那将非常快。
require"connection.php";
上面的行使用了两次是错误的,而是使用全局变量来获取函数内部的DB连接。