我在我的Android应用程序中有这两个活动,其中第一个是用户输入询问信息的地方(到edittext
),另一个是它将发送数据的地方(我用过{ {1}}将数据从第一个活动传输到第二个活动到MySQL数据库,稍后将在putExtra
显示结果。问题是,当第二个活动开始时(考虑到我已经在第一个活动上输入了某些内容)并且在进度对话框显示之后,没有显示任何内容,或者结果没有显示。但是当我尝试启动第二个活动(第一个活动中的ListView
为空)时,它会显示结果。我不确定导致问题的原因是应用程序还是用于获取数据的PHP文件?
以下是代码:
MainActivity.java
edittext
SearchResultsAcivity.java
//first activity
public class SearchFragment extends Fragment implements View.OnClickListener {
Button butt;
EditText destination;
String d;
public SearchFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_search, container, false);
butt = (Button) view.findViewById(R.id.searchBUTTon);
butt.setOnClickListener(this);
destination = (EditText) view.findViewById(R.id.destinationTO);
return view;
}
@Override
public void onClick(View v) {
d = destination.getText().toString();
Intent a = new Intent(getActivity(), SearchResultsActivity.class);
a.putExtra("to", d);
startActivity(a);
}
}
RequestHandler.java
//second activity
public class SearchResultsActivity extends AppCompatActivity implements ListView.OnItemClickListener {
private ListView listView;
private String JSON_STRING;
String destination;
TextView d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
Intent a = getIntent();
destination = a.getStringExtra("to");
d = (TextView) findViewById(R.id.textView3);
d.setText(destination);
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
getJSON();
}
private void showBusList() {
JSONObject jsonObject = null;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(config.TAG_JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
//get strings
String id = jo.getString(config.TAG_ID);
String busName = jo.getString(config.TAG_BUSNAME);
String terminal = jo.getString(config.TAG_TERMINAL);
HashMap<String, String> busDetails = new HashMap<>();
busDetails.put(config.TAG_ID, id);
busDetails.put(config.TAG_BUSNAME, busName);
busDetails.put(config.TAG_TERMINAL, terminal);
list.add(busDetails);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
SearchResultsActivity.this, list, R.layout.result_list_item, new String[] {
config.TAG_ID, config.TAG_BUSNAME, config.TAG_TERMINAL}, new int[] {R.id.id, R.id.busName,
R.id.terminal});
listView.setAdapter(adapter);
}
private void getJSON() {
class GetJSON extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(SearchResultsActivity.this, "Message", "Fetching data... Please wait.", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showBusList();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequestParam(config.URL_SEARCH, destination);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
PHP文件
//handles requests
public String sendGetRequestParam(String requestURL, String id){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+id);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"\n");
}
}catch(Exception e){
}
return sb.toString();
}
来自logcat的错误
<?php
$connection = mysqli_connect("mysql.hostinger.ph", "u679871488_bus", "Damnyoufudge20", "u679871488_bus") or die("Error " . mysqli_error($connection));
$des = $_GET['destination'];
$sql = "SELECT * from appDB WHERE route LIKE '%".$des."%'";
$result = mysqli_query($connection, $sql) or die ("Error in Selecting " . mysqli_error($connection));
$thisArray = array();
while($row = mysqli_fetch_assoc($result)) {
$thisArray[] = $row;
}
echo json_encode(array('busDetails' => $thisArray));
答案 0 :(得分:0)
在PHP代码中使用php中的array_push
方法
while($row = mysqli_fetch_assoc($result)) {
//$thisArray[] = $row;
array_push($thisArray, $row);
}
我认为它会起作用。 尝试一次并检查是否在android中获取此数据。
将用户输入直接附加到php中的sql查询是不好的做法,就像使用'%".$des."%'
一样。它会导致SQL Injection Attacks
。
始终倾向于mysqli_prepare($sql)
以避免SQL Injection Attacks
。
更新1
在SearchResultsActivity.java
中,尝试替换
destination = a.getStringExtra("to");
这一行
destination = a.getExtras().getString("to");
Log.e("tag", " DESTINATION :: " + destination);
并检查您是否从第一项活动中获取了该文字。
更新2
永远不要忘记你投掷的catch
例外情况。
您忘记在sendGetRequestParam
方法中捕获异常。抓住并打印出来。因此,您将知道连接到服务器是否有任何错误。