Ich habe eine Seite erstellt, die Daten zeigt, die in der Mysql DB vorhanden ist. Ich habe PHP für die Verbindung verwendet. Die PHP-Datei enthält eine Select-Abfrage, um die in der Datenbank vorhandenen Daten anzuzeigen. PHP-Datei wird von Android-Code aufgerufen. Nach Abschluss der JSON-Analyse sollten die Daten in der App angezeigt werden. Aber das Problem ist, dass Daten nicht in der App abgerufen werden.retrive Daten von Mysql zu Android mit PHP
Hier, Es gibt keinen einzigen Fehler, den ich in Java-Code bekomme. Außerdem habe ich PHP-Datei localhost ausgeführt, es funktioniert völlig gut und in der Ausgabe, ich bekomme die JSON-Daten.
Die einzige Sache ist, dass JSON nicht auf der APP zurückkommt. Bitte hilf mir. Ich stecke hier fest. Ich habe den ganzen Tag dafür versucht und habe nichts gefunden. Ich brauche Hilfe Jungs !!! Bitte, wenn Sie etwas herausfinden, lassen Sie es mich wissen.
Code:
See_Issue.java (where data will retrive from DB)
package com.example.mi.mikpiadmin;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class See_Issue extends AppCompatActivity implements ListView.OnItemClickListener {
private ListView listView;
private String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.see_feedback);
listView=(ListView)findViewById(R.id.list_view) ;
listView.setOnItemClickListener(this);
getJSON();
}
private void showEmployee(){
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_ISSUE_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String storename = jo.getString(Config.TAG_STORE_NAME);
String issue = jo.getString(Config.TAG_ISSUE);
HashMap<String,String> employees = new HashMap<>();
employees.put(Config.TAG_STORE_NAME,storename);
employees.put(Config.TAG_ISSUE,issue);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
See_Issue.this, list, R.layout.list_item,
new String[]{Config.TAG_STORE_NAME,Config.TAG_DESCRIBE},
new int[]{R.id.editTextstorename, R.id.editTextdescribe});
listView.setAdapter(adapter);
}
private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String> {
private ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(See_Issue.this,"Fetching Data","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showEmployee();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
return rh.sendGetRequest(Config.URL_GET_ISSUE);
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, See_Issue.class);
HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
String empId = map.get(Config.TAG_ISSUE_ID).toString();
intent.putExtra(Config.EMP_ID,empId);
startActivity(intent);
}
}
Config.java
package com.example.mi.mikpiadmin;
public class Config {
public static final String URL_GET_ALL = "http://10.238.4.166/new/one.php";
public static final String URL_GET_ISSUE = "http://10.238.4.166/new/see_issue.php";
//public static final String URL_GET_EMP = "http://10.238.4.166/new/getFeedback.php?id=";
//Keys that will be used to send the request to php scripts
public static final String KEY_EMP_ID = "id";
public static final String KEY_EMP_STORE_NAME = "storename";
public static final String KEY_EMP_NAME = "name";
public static final String KEY_EMP_FEEDBACK = "feedback";
//JSON Tags
public static final String TAG_JSON_ARRAY="result";
public static final String TAG_ID = "id";
public static final String TAG_STORENAME = "storename";
public static final String TAG_NAME = "name";
public static final String TAG_FEEDBACK = "feedback";
//employee id to pass with intent
public static final String EMP_ID = "emp_id";
public static final String TAG_JSON_ISSUE_ARRAY="result";
public static final String TAG_ISSUE_ID = "id";
public static final String TAG_STORE_NAME = "storename";
public static final String TAG_ISSUE = "issue";
public static final String TAG_DESCRIBE = "describe";
}
getIssue.php
<?php
//Importing Database Script
require_once('dbConfig.php');
//Creating sql query
$sql = "SELECT * FROM user_issue";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"store_name"=>$row['store_name'],
"issue"=>$row['issue'],
"describe"=>$row['describ']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
RequestHandler.java
package com.example.mi.mikpiadmin;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class RequestHandler {
//Method to send httpPostRequest
//This method is taking two arguments
//First argument is the URL of the script to which we will send the request
//Other is an HashMap with name value pairs containing the data to be send with the request
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
//Creating a URL
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
while ((response = br.readLine()) != null){
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public String sendGetRequest(String requestURL){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL);
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();
}
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();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
hast du deinen PHP Code überprüft? Sie müssen Schritt für Schritt gehen. –
Sind Sie sicher, dass es nicht nur ein Tippfehler ist? 'getIssue.php'! ==' see_issue.php' – jeroen
@jeroen: Entschuldigung, es ist ein Tippfehler –